Friday, January 24, 2020

How to get All parent objects and child objects related to the current object

Assume Opportunity is the current object, you want to know all parent object and child objects info of the current object.

All Parent Objects

Sample Code
for(Schema.SobjectField iterator: Opportunity.SobjectType.getDescribe().fields.getMap().Values()) {
    if(iterator.getDescribe().getType() == Schema.DisplayType.REFERENCE) {
        system.debug('Parent Objects ====> '+iterator.getDescribe().getReferenceTo());


All Child Objects

Sample Code
Schema.DescribeSObjectResult sObjectInfo = Opportunity.SObjectType.getDescribe();
for (Schema.ChildRelationship iterator: sObjectInfo.getChildRelationships()) {
  system.debug('Child Objects ====> '+iterator.getChildSObject());

No comments:

Post a Comment