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:
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