Search This Blog

Friday, October 13, 2017

How to get all the child objects related to an object in Salesforce

Solution : By using getChildRelationships() ,we can fetch all the child objects associated to an object.
In this example,we fetch all the child objects associated to Account object.
Sample Apex Code:
Set<String> lstObject = new Set<String>();
Schema.DescribeSObjectResult sObjResult = Account.SObjectType.getDescribe();
for(Schema.ChildRelationship cr: sObjResult.getChildRelationships()) {
    lstObject.add(string.valueOf(cr.getChildSObject()));
}
system.debug('Child Objects associated with Account:'+lstObject);
Output: When we run the above code in workbench,output will be like this:


No comments:

Post a Comment