If i have a query like:
SELECT id, (SELECT id FROM Detail_Object__r)
FROM Master_Object__c
WHERE id IN: trigger.newmap.keyset()
Is it possible to assign the results to a list of type Detail_Object__c directly?
Attribution to: Phil B
Possible Suggestion/Solution #1
List<Detail_Object__c> details = [SELECT Id, Name
FROM Detail_Object__c
WHERE Lookup_to_Master__c IN :trigger.new];
No need to even extract the Ids into Set with trigger.newMap.keyset(); bind variable should work with collection of sObjects.
Attribution to: eyescream
Possible Suggestion/Solution #2
You could also iterate over them, after you did your query, this way you will have all the results, regardless of how many master objects.
like so:
List<Detail_Object__c> detailObjectList = new List<Detail_Object__c>();
for(Master_Object__c m : masterObjectList)
{
detailObjectList.addAll(m.Detail_Object__r);
}
Attribution to: pjcarly
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3725