Populating a Parent Object lookup field with multiple child

Posted by Rollbase User on 06-Jul-2012 12:39

I have two objects. (1) The Parent Object and (2) The Child Object. I want to use an Update Field Value Trigger to populate the lookup field of the parent object with all records in the Child Object that have the Workflow Status of "Open". The formula for my trigger is as follows: rbv_api.selectNumber("SELECT id FROM scholarship1 WHERE status='Open'") where scholarship1 is the child object. This formula seems to return only the first occurrence of a record in the child object with the status of "Open". How can I use a formula to populate the lookup field with multiple related records that match the condition in the WHERE clause?

All Replies

Posted by Admin on 06-Jul-2012 12:51

selectNumber API will fetch you only one ID. I think you need array of IDs, so use selectQuery API (see Chapter 6 for examples)

Posted by Admin on 06-Jul-2012 12:59

After reading about selectQuery, I changed my Update Field Value formula to this:



var x = rbv_api.selectQuery("SELECT id FROM scholarship1 WHERE status='Open'",100);



return x;



This does not seem to work either and, according to the trigger debugger, the above formula returns null.



Thanks!



Posted by Admin on 06-Jul-2012 14:08

UPDATE: I removed the WHERE clause all together for testing purposes, and used the trigger debugger after changing the formula to this:



I was hoping to put the contents of the array into a string, separated by commas, then return.



When I run the trigger debugger, the trigger updates the lookup field value to "-1,-1,-1"



This tells me that the trigger knows that there are 3 records in scholarship1 (the child object), but returns -1 for the id value for each record.



Just to reiterate, this is an "Update Field Value" trigger running on the parent object "after create". I want to update the lookup field value of the parent object to all ids of the child object (scholarship1).



Am I referencing id incorrectly?



Posted by Admin on 06-Jul-2012 14:12

The formula won't display correctly because the "less than" sign in the for loop is being mistaken for an HTML tag, below is the formula with the less than sign replaced with the words "less than":





var x = rbv_api.selectQuery("SELECT id FROM scholarship1",100);



var y = "";



for (i=0;i less than x.length;i++){

y += x[i] + ","

}



return y;

Posted by Admin on 09-Jul-2012 08:48

Do you guys have any suggestions for this one? Is my use of selectQuery correct above?

Posted by Admin on 09-Jul-2012 11:42

Please try again after release this Saturday: I can see some issue here.

Posted by Admin on 09-Jul-2012 13:54

Just an update as I was able to get it to work and discovered something.



Using the debugging tools I discovered that rbv_api.selectQuery() ALWAYS returns a 2 dimensional array, even if only one field is selected in the query. In the formula above, the rbv_api.selectQuery() call returned:



{ { 618181, }, { 618186, }, { 618187, }, }



which explains why the return value was incorrect.



I changed "x[i]" in the for loop written above to "x[i][0]" and was able to get the results I needed.



Posted by Admin on 09-Jul-2012 13:59

Yes, selectQuery always returns 2d array: Chapter 6, page 29.

Posted by Admin on 10-Jul-2012 23:48

I'll make changes in the next release allowing arrays of IDs to be used in triggers.



Consider this example:



var arr = rbv_api.selectQuery("SELECT id FROM contact WHERE status=?", 'Ready');

var ids = new Array();

for (var k=0; k

ids[k] = arr[k][0];

}

return ids;

This thread is closed