+ Start a Discussion
Jared RosenbergJared Rosenberg 

Referencing sObject within another sObject

Good afternoon, folks.

I'm in the process of trying to consolidate the SOQL calls within a trigger of a particularly difficult sObject.  This sObject has enough code running on it that we're pretty regularly hitting the 101 error--so it's time to clean up and bulkify where possible.

I've run the following to fill a map with an sobject, and the children of that sobject.  Let's assume for the moment that Service_Order_Line is a child of Service_Order with a many-to-one relationship, and I've filtered this down so I'm only getting one Service_Order and maybe 5 or 10 Service_Order_Lines:

map<ID, Service_Order__c> testResults = new map<ID, Service_Order__c>([select ID, Name, (select ID, Name from Service_Order_Line__r) from Service_Order__c']);

Further down in the trigger, I'll need to reference the child objects, but I can't seem to find a syntax to make it work.  How would I write the following to loop through the children of testResults?

for(Service_Order_Line childrenOfTestResults : testResults.Service_Order_Line__r) {
    system.debug(childrenOfTestResults.Name);
}
Best Answer chosen by Jared Rosenberg
Greg HGreg H
I believe the issue is that you are missing the __c on the "childrenOfTestResults" variable declaration in the for loop. Use Service_Order_Line__c instead of Service_Order_Line. You also need to be iterating over the parent Service_Order__c records before you can grab the children. Something like this:
for (Service_Order__c so : testResults.values()) { //for all values in the testResults map
    System.debug('Service_Order__c: '+so.Name+' ('+so.Id+')'); //display the Service_Order__c name (Id) in the debug log
    for (Service_Order_Line__c sol : so.Service_Order_Line__r ) { //for each Service_Order_Line__c related to the Service_Order__c
        System.debug('Service_Order_Line__c: '+sol.Name+' ('+sol.Id+')'); //display the Service_Order_Line__c name (Id) in the debug log
    }
}
Hope this helps.
-greg

All Answers

Jared RosenbergJared Rosenberg
Just in case it comes up, I've tried all 3 of testResults.Service_Order_Line, testResults.Service_Order_Line__c, and testResults.Service_Order_Line__r.  I'm not sure whether it retains the custom object suffixes, but all three of them fail with the same error saying the variable doesn't exist.
Greg HGreg H
I believe the issue is that you are missing the __c on the "childrenOfTestResults" variable declaration in the for loop. Use Service_Order_Line__c instead of Service_Order_Line. You also need to be iterating over the parent Service_Order__c records before you can grab the children. Something like this:
for (Service_Order__c so : testResults.values()) { //for all values in the testResults map
    System.debug('Service_Order__c: '+so.Name+' ('+so.Id+')'); //display the Service_Order__c name (Id) in the debug log
    for (Service_Order_Line__c sol : so.Service_Order_Line__r ) { //for each Service_Order_Line__c related to the Service_Order__c
        System.debug('Service_Order_Line__c: '+sol.Name+' ('+sol.Id+')'); //display the Service_Order_Line__c name (Id) in the debug log
    }
}
Hope this helps.
-greg
This was selected as the best answer
Jared RosenbergJared Rosenberg
@Greg H - Worked a treat.  Thanks for your assistance.  I hadn't been looping through the parent.