Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created June 1, 2019 03:04
Show Gist options
  • Save Sunil02kumar/602e36e7da5c10e14ea4d7a25c29be11 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/602e36e7da5c10e14ea4d7a25c29be11 to your computer and use it in GitHub Desktop.
How to get List of All Child Records for Given Parent Records in .csv File using APEX
string parentObjectName ='Account';//Specify Object API Name
string parentRecordId = '0019xxxxxxx7MqU'; //Specify record Id
//to store childRelationShipName
Map<integer,List<string>> childObjectmap = new Map<integer,List<string>>();
//to store inner query string for child objects
Map<integer,List<string>> childObjectQStringmap = new Map<integer,List<string>>();
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
for(String ss1: schemaMap.keyset()){
Schema.SObjectType objToken=schemaMap.get(ss1);
if(ss1.equalsignorecase(parentObjectName)){
//find details about sobject
Schema.DescribeSObjectResult objDescribe=objToken.getdescribe();
List<Schema.ChildRelationship> childRelationshipList = objDescribe.getChildRelationships();
system.debug('*********childRelationshipList size:'+childRelationshipList.size());
for(Schema.ChildRelationship ss:childRelationshipList){
if(ss.getRelationshipName()!=null){
string RelationshipName=ss.getRelationshipName();
string childObjName = string.valueof(ss.getChildSObject());
//Check if child object is isQueryable() or not
Schema.DescribeSObjectResult childObjDescribe = schemaMap.get(childObjName).getdescribe();
system.debug('*******childObjName:'+childObjName);
//system.debug('*********isRestrictedDelete():'+ss.isRestrictedDelete());
//system.debug('*********RelationshipName:'+RelationshipName);
if(childObjDescribe.isQueryable()){
if(childObjectmap.size()==0){
childObjectmap.put(1,new List<string>());
childObjectmap.get(childObjectmap.size()).add(RelationshipName);
childObjectQStringmap.put(1,new List<string>());
childObjectQStringmap.get(childObjectQStringmap.size()).add('(select id from '+ RelationshipName + ' )');
}else{
if(childObjectmap.get(childObjectmap.size()).size()<20){
childObjectmap.get(childObjectmap.size()).add(RelationshipName);
childObjectQStringmap.get(childObjectQStringmap.size()).add('(select id from '+ RelationshipName + ' )');
}else{
childObjectmap.put(childObjectmap.size()+1,new List<string>());
childObjectmap.get(childObjectmap.size()).add(RelationshipName);
childObjectQStringmap.put(childObjectQStringmap.size()+1,new List<string>());
childObjectQStringmap.get(childObjectQStringmap.size()).add('(select id from '+ RelationshipName + ' )');
}
}
}else{
system.debug('******'+childObjName+' object is not querable.');
}
}
}
}
}
system.debug('***childObjectmap.size():'+childObjectmap.size());
system.debug('***childObjectQStringmap.size():'+childObjectQStringmap.size());
string header = 'Related Object name , Records deatils \n';
string finalstr = header ;
try{
for(integer i:childObjectmap.keyset()){
string qString = 'Select id,'+ string.join(childObjectQStringmap.get(i),',') +' from ' +parentObjectName+ ' where Id=:parentRecordId LIMIT 1';
system.debug('***qString:'+qString);
sObject records = database.query(qString);
for(string childObjectRec:childObjectmap.get(i)){
string childRecordId ='';
for(sobject ch:records.getSObjects(childObjectRec)){
childRecordId = childRecordId +' '+ ch.get('Id');
}
//string recordString= childObjectRec + ',' + records.getSObjects(childObjectRec) + '\n';
string recordString= childObjectRec + ',' + childRecordId + '\n';
finalstr = finalstr + recordString;
}
}
}catch(exception ex){
system.debug('******exception:'+ex.getmessage());
}
string OrgURL = URL.getSalesforceBaseURL().toExternalForm();
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= parentObjectName + '(' + parentRecordId + ') Related child records.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {UserInfo.getUserEmail()};
String subject =parentObjectName + '(' + parentRecordId + ') Related child records Report - Org URL:'+OrgURL;
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody(subject);
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment