0

Can someone tell me how I can access the data from child object via Apex from a relationship SOQL ?

This is my SOQL

SELECT Opportunity.Id, Opportunity.Max_Loan_Amount__c, (SELECT Application__c.Id, Application__c.Loan_Amount__c FROM Opportunity.Applications__r) FROM Opportunity WHERE Max_Loan_Amount__c > 0

Opportunity is the parent and Application__c is the child.

This is the screenshot from my dev console and as you can see it returns only one record.

enter image description here

Any pointers or links would be greatly appreciated.

2 Answers 2

2

You can access like below

List<Opportunity> oppsList = SELECT Opportunity.Id, Opportunity.Max_Loan_Amount__c, 
 (SELECT Id,Loan_Amount__c FROM Opportunity.Applications__r) 
    FROM Opportunity WHERE Max_Loan_Amount__c > 0

    //For each opportunity
    for(Opportunity opp:oppsList){
         //checking applications list size 
         if(opp.Applications__r.size()>0){
            //looping thru each application
            for(Application__c app:opp.Applications__r){
                 //access application info using app.Id, app.Name here
            }
         }
    }
1

you can use the Relationship name to specify the child object in the SOQL inside APEX.

List<Opportunity> oppsList = SELECT Opportunity.Id, Opportunity.Max_Loan_Amount__c, 
 (SELECT Id,Loan_Amount__c FROM Opportunity.Applications__r) 
FROM Opportunity WHERE Max_Loan_Amount__c > 0

and then when you want to access the child object list, you can use

oppsList[index].Applications__r[index]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .