0

I have the following piece of code

 public static void fetchRelatedRecords(String primaryRecordString){
    SObject primaryRecord = (SObject)JSON.deserialize(primaryRecordString, SObject.class);
    String recordId = (String)primaryRecord.get('Id');
    Map<String, Schema.SobjectType> keys = new Map<String, Schema.SobjectType>();
    Map<String, Schema.SobjectType> describe = Schema.getGlobalDescribe();
    for(String s : describe.keyset()){
        keys.put(describe.get(s).getDescribe().getKeyPrefix(),describe.get(s));
    }

    Schema.SObjectType sobjectTypeObj = keys.get(recordId.substring(0,3));        
    Schema.DescribeSObjectResult obj = sobjectTypeObj.getDescribe();        
    String query = 'select Id, ';
    for (Schema.ChildRelationship cr: obj.getChildRelationships()){
        if (cr.getRelationshipName() == null){ 
            continue;
        }
        else{
            system.debug('Child Object Relationship Name:'+cr.getRelationshipName());
            query = query + '(select Id from ' + String.valueOf(cr.getRelationshipName()) + '), '; 
        }
    }
    query = query.removeEnd(', ');
    query = query + ' from ' + String.valueOf(sobjectTypeObj) + ' where Id = ' + '\'' + recordId + '\'';
    List<SObject> relatedRecordList = Database.query(query);
}

My problem here is that it is giving me the following error

System.UnexpectedException: SOQL statements cannot query more than 20 different child types.

Is there any way to by pass this governor limit?

2
  • 1
    What about making 2 (or n) queries?
    – kurunve
    Feb 12 '20 at 15:02
  • I tried making n queries, but, then I get to a point where I have to do queries within a for loop, which again goes against the best practise. Is there any way to do n queries without a loop? @kurunve
    – Madhurima
    Feb 12 '20 at 15:04
1

This is a documented limit on SOQL parent-child queries:

No more than 20 parent-to-child relationships can be specified in a query.

You'll have to batch your queries in groups of 20 parent-child relationships.

2
  • I tried to put it in separate queries, 20 at a time. But a point comes when I have to run the separate queries within a for loop, which goes against the best practise. Is there any way to overcome that?
    – Madhurima
    Feb 12 '20 at 15:18
  • It's not best practice to run queries in a loop when they can be bulkified. However, since you cannot bulkify these queries beyond batching in 20s, it is the required implementation pattern - unless you can reduce the scope of data you need to retrieve.
    – David Reed
    Feb 12 '20 at 15:41
0

It's governor limit and not possible to by-pass.

But getRelationshipName() returns all the childs including standards ones like

Events,ProcessInstances,Processsteps,Feeds, Topics, RecordActionHistories and list goes on

. Actually you might not need most of them. I would suggest you to review the child which is been received form the getRelationshipName and then include only the necessary ones.

You can also store the necessary child names the static variable or a custom label

and check before adding them to the query.

Hope this helps!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.