Last active
May 30, 2019 02:24
-
-
Save Sunil02kumar/0faf96afb2f300eaf225be431578afe9 to your computer and use it in GitHub Desktop.
How to get all Child Relationship Names related to Parent Object using Apex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String parentObjectName = 'Account'; | |
| 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment