I need to create a Rest Api where I want to take as input an Id and perform a query on an object. The response must contain the fields of this record and also some related records (children) and the related of the related records (grandchildren). Idealy, i would like to do something like this:

           String queryString = 'select id, name, (select id, name, (select id, name from 
           csord__Assets__r) from csord__Services__r) from csord__Subscription__c where id =:recordId';
List<csord__Subscription__c> subscrList = Database.query(queryString);
if(subscrList.size() >0) {
  currentResult.subscriptionResult = subscrList;

but Salesforce does not allow to query aggregate relationships more than 1 level away from the root entity object. However, i can't find a "clever" way to get the required result. Has anyone faced the same issue? What could be the alternatives to resolve the issue and return a proper response ?

1 Answer 1


You'll need to build two wrapper classes that look something like this:

public class ChildWrapper {
    // Option 1: store the entire child object record
    public ChildObj__c theChild { get; set; }

    // Option 2: track each child  field separately
    public Id childId { get; set; }
    public String childName { get; set; }
    // etc. etc.

    public List<GrandchildObj__c> theGrandchildren { get; set; }

    public ChildWrapper() {}

public class ParentWrapper {
    // Option 1: store the entire parent object record
    public ParentObj__c theParent { get; set; }

    // Option 2: track each parent field separately
    public Id parentId { get; set; }
    public String parentName { get; set; }
    // etc. etc.

    public List<ChildWrapper> children { get; set; }

    public ParentWrapper() {}

Then you must perform separate queries to get your parent-child records and your child-grandchild records, and use maps and loops to populate the wrappers. Then you can return the ParentWrapper as your response to the API call.

  • Hi David and thanks for the response. My "problem" is that i get a response of this structure {ParentWrapper -> theParent -> children}, {ChildWrapper -> theChild -> theGrandChildren} whereas i am trying to get something like: Parent -> Children -> Grandchildren. Is there a way to show in the response directly the three-levels ? Commented Jan 30, 2021 at 16:57
  • You have to use wrapper classes. Commented Jan 31, 2021 at 0:10
  • With wrapper classes i couldn't have in a nested way the Parent -> Child -> Grandchild in the response of the Service. What seems really useful that i found after a lot of research is this. cloudgiants.com/blog-detailed/2016/8/2/…. Can this be achieved with wrapper classes? Commented Jan 31, 2021 at 0:24
  • 1
    I edited my answer. Regardless of which option you choose, the wrapper hierarchy is exactly the same as what you you want. ParentWrapper <- ChildWrappers < GrandchildWrappers is the same as Parent <- Children <- Grandchildren. Commented Jan 31, 2021 at 2:01

You must log in to answer this question.

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