function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris Sanchez 5Chris Sanchez 5 

SOQL Relationship in Javascript

Hello,  I have the following javascript in a button on the opportunity window which I believe works as the alert does display correctly.  I havent been able to confirm if the child query portion is working correctly.  How can I alert the select part of the child query?  For example something like, var groupName =  records[0].Groups__r.Name, then I would like to alert groupName.  I appreciate your help.


{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var groupID = sforce.connection.query("select Name, Id,(Select ID, Name, Head_Count__c FROM Groups__r) FROM Opportunity Limit 1"  );

var records = groupID.getArray("records"); 

var Idee = records[0].Id; 

var nam = records[0].Name;

alert(Idee + ' ' + nam);
Raj VakatiRaj Vakati
var grp= records[0].Group__d;

alert(grp+ ' ' + grp);

 
Raj VakatiRaj Vakati
Use this code


var grp= records[0].Groups__r; alert(grp+ ' ' + grp);


Chris Sanchez 5Chris Sanchez 5
Thanks for the reply. That looks like it’s working. How would I just show only one of the values in the child query. For example if I only wanted to see Head_Count__c? Thanks, Chris
Chris Sanchez 5Chris Sanchez 5
Update:
There are 3 group records related to my test opportunity.  When I run my query all 3 groups are placed in the the first result returned, in the 0 position in the array.  How can I rewrite the query so I get a seperate result for each group?  For example, records[2].Name would actually return a value?
Raj VakatiRaj Vakati
You need to use JavaScript as shown below 
var records = groupID.getArray("records"); 
for (var j = 0; j < records[0].Groups__r.length; j++) 
 { 
   var res = records[0].Groups__r[j].Head_Count__c;
   alert(res);
}