I am trying to create a Map<Id, List<sObject>>
where Id
= Parent Object Id and List<sObject>
is a list of child objects from a specified master-detail relationship.
In SOQL, this is equivalent to:
SELECT Id, Name, (SELECT Id, Name FROM Relationship_Name__r) FROM Parent_Object_API_Name__c
I am looking for the best way in apex to do this and not via SOQL. I looked at this and this .
String relationship_name = 'Relationship_Name__r';
for(sObject item : items)
List<sObject> children = item.getSObjects(relationship_name);
This returns a null
value for children list.
for(sObject item : items)
List<sObject> children = item.Relationship_Name__r;
This neither returns a null nor the children i.e., children.size() = 0
.
I would like to specify the relationship name in a String variable (so 2nd approach above is not ideal).
Is it possible?