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?