-2

I need to know which all objects have parent as Opportunity. Is there any easy way to get the details?

0

3 Answers 3

1

You can use the DescribeSObjectResult class to access any SObject parent and/or child objects. Here is a print out of child objects of the Opportunity SObjectType.

Schema.DescribeSObjectResult sobResult = Opportunity.SObjectType.getDescribe();
for (Schema.ChildRelationship childRel : sobResult.getChildRelationships()) {
    System.debug('Child Object: ' + childRel.getChildSObject());
}
0

Try this: Opportunity.getSObjectType().getDescribe().getChildRelationships(); Edit: Ouh, @TSmith answered before me ;D Gj!

1
  • 1
    hey David! Welcome to SFSE! thanks for taking the time to provide answers, however, if you could please avoid posting 1 liners and help provide an explanation as to "why" trying this answers the OP's question, it would be more helpful to the community and better received overall. Thanks!
    – glls
    Commented May 5, 2021 at 13:18
0

Try this Code to fetch the child objects which have the opportunity as parent object

String parentObjectName = 'Opportunity';  
Map<string,string> objectRelationshipMap = new Map<string,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){
            string RelationshipName=ss.getRelationshipName();
            string childObjectName = string.valueof(ss.getChildSObject());
            string childFieldToken = string.valueof(ss.getField());
            system.debug('*********childFieldToken:'+ childFieldToken);
            system.debug('*********childObjectName:'+childObjectName); 
            system.debug('*********RelationshipName:'+RelationshipName);
            objectRelationshipMap.put(childObjectName+'.'+childFieldToken,RelationshipName);
        }
    }
}
system.debug('***objectRelationshipMap.size():'+objectRelationshipMap.size());
system.debug('***objectRelationshipMap:'+objectRelationshipMap);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .