Creating a "count" of related records, and "f

Posted by Rollbase User on 27-Feb-2012 20:29

Hi, I need some help in creating a "counter" field, and then assign the count to a field I have the following objects PortalUsers, Sessions, and Elements. - 1 PortalUser can have many Sessions - 1 Session can have many Elements When a new element is created, I want to count the number of "elements that exist for the related session," and put that number in a field called "Element #." This means that, for the 1st element (for a session) that is created, Element # =1 (this number should now be "fixed" at this value for this record) The 2nd element (for a session) that is created, Element # =2 (this number should now be "fixed" at this value for this record) The 3rd element (for a session) that is created, Element # =3 (this number should now be "fixed" at this value for this record) etc. I've tried many things, but haven't been able to get the "Element #" to remain fixed after it's been calculated in a record. I end up with the Element # of all "Elements" records related to the "moth

All Replies

Posted by Admin on 27-Feb-2012 22:36

Hi,



When using count of all elements of a session, which record is being edited and created or updated ? Is it the session or the element being used? Where is your trigger to update this field ? Is it in the parent session or the child elements? Can you paste your code here so I can review.



Thanks

Piscoso Martin

Posted by Admin on 28-Feb-2012 19:29

Hi Martin, Thanks for your help.



I tried several things. I created a "count" record (formula) in the "Sessions" object. This means that the value in the "session" record is changing every time that an element is added.



#CALC_COUNT.R47184800( 1 | true ) --- this is the formula of the "count field" in the the "sessions" object



I would like to capture the number then, and copy it into the "number of elements" field in the "Elements" object (and make it fixed -- not changing the next time a new element is added)



My trigger to update the field is in the object "Sessions", but I realize that I don't know how to get it triggered. I tried to use an action in the "Elements" object, but the "trigger in Sessions" is not available.



Thanks again for helping me wrap my head around how this all comes together.



Marc

Posted by Admin on 28-Feb-2012 22:59

Hi,



Your trigger should be on the after create of the child elements rather than the parent session . As it is now, it will update all related child elements.



On your parent session add a field countofelements and place your count group token inside, #CALC_COUNT.R47184800( 1 | true ) use an integer return type.



Now on the trigger on your child elements after create , get the related parents countofelements token and save that onto the desired field. Use the drop down helper there and search for the related session.



Thanks.

Posted by Admin on 01-Mar-2012 17:34

Hi Martin,

I believe we're getting closer.



The trigger is on the "after create" of the child Elements.



-------



The countofelements in the parent Session calculates well -- I have what you suggested.



Question: you mentioned "get the related parent group count token,"

are you talking about this: {!R47184800.countofelements}



Then you say "save that onto the desired field." Is the formula something like this?

rbv_api.setFieldValue('session2','{!R47184800.id}', 'countofelements', '{!R47184800.countofelements}');



If I understand well, I should put the info related to the parent Session related record?



-------------------------------------



When I insert a new element (using the portal page), I get the following error message:



Element "Element 3 test Jack Black" has been created.



Error executing Trigger: "CopySessionElementsCount" Error Field countofelements is read-only (line #3) in formula:var rbv_api = new Packages.com.rb.core.services.api.ServerSideAPI(47181060, 47184353

Posted by Admin on 01-Mar-2012 21:51

rbv_api.setFieldValue('session2','{!R47184800.id}', 'countofelements', '{!R47184800.countofelements}'); <



rbv_api.setFieldValue('element','{!id}', 'someotherfield', '{!R47184800.countofelements}');



Since your on the element record and not the session.

Basically, your getting it on the session and placing it on the element =)

Posted by Admin on 02-Mar-2012 16:07

Hi Martin,

I have done what you proposed, and I am still getting the following error message. I am trying to read what it means, but it's not clear to me. Could you help me understand the error, and fix this?



Here is the error message I get after clicking the "submit button" on the Element creation "portal page."



---------



Element "Element 1 Jack Black" has been created. ====> the element is created



Error executing Trigger: "CopySessionElementsCount" Error missing ) after argument list (line #3) in formula:var rbv_api = new Packages.com.rb.core.services.api.ServerSideAPI(47181060, 47184353);rbv_api.setFieldValue('element1','48074540, 'sessionelementnumber', '1');



-----



Let's cut the Error Message into pieces to understand the meaning:



Error executing Trigger: "CopySessionElementsCount" ==> the"CopySessionElementsCount" trigger is the problem



Error missing ) after argument list (line #3) in formula: ==> I went into the trigger definition, and I don't think there is a ")" missing



Posted by Admin on 02-Mar-2012 20:52

Hi,



Based on your error received it seems to be a syntax issue.



Something that might help would be to change the Trigger Type of your after create trigger to an Update Field Value type of trigger.



>> instead of using this >>

rbv_api.setFieldValue('element1','48074540, 'sessionelementnumber', '1');



Delete your current Trigger and create a new 'Update Field Value' Trigger >> Set the field to be updated to >> sessionelementnumber >> set it to run after create >>



then use this code instead >>

return {!R123123.countofelements}; //from parent - change this to your actual field.



This is easier to handle and easier to debug since you are only trying to update one field.



Thanks

Piscoso Martin

This thread is closed