function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
병훈 송병훈 송 

How can I find Salesforce's every relationship?

How can I find Salesforce's every relationship?

I want create Master-Detail Field or LookUp Relationship Field by "Salesforce Metadata Api"
Problem occurred here!

When Child Relationship Name duplicated, that make error.
So.....First, I find every RelationShip by Salesforce Query and,
I want Find overlapping Child Relationship Name..

How can I do it?
Best Answer chosen by 병훈 송
monsterloomismonsterloomis
If you just need to see the child relationships for every object, you can run an anonymous block like the one below to look at the schema and then filter the logs for "debug" to see the results. If you want to tinker with it, I recommend reviewing the relevant class(es) so you can zero in on what you need: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_ChildRelationship.htm#apex_class_Schema_ChildRelationship:
 
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
String childRelationships = '';
for (SObjectType so:gd.values()) {
    childRelationships += '\n\n PARENT OBJECT: ' + so.getDescribe().getLocalName().toUpperCase();
	for (Schema.childRelationship cr:so.getDescribe().getChildRelationships()) {         
		childRelationships += '\n child object name: ' + cr.getChildSObject().getDescribe().getLocalName()
            				+ '\n relationship name: ' + cr.getRelationshipName();
	}    
}
System.debug('RELATIONSHIPS: ' + childRelationships);

Good luck!

All Answers

monsterloomismonsterloomis
If you just need to see the child relationships for every object, you can run an anonymous block like the one below to look at the schema and then filter the logs for "debug" to see the results. If you want to tinker with it, I recommend reviewing the relevant class(es) so you can zero in on what you need: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_ChildRelationship.htm#apex_class_Schema_ChildRelationship:
 
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
String childRelationships = '';
for (SObjectType so:gd.values()) {
    childRelationships += '\n\n PARENT OBJECT: ' + so.getDescribe().getLocalName().toUpperCase();
	for (Schema.childRelationship cr:so.getDescribe().getChildRelationships()) {         
		childRelationships += '\n child object name: ' + cr.getChildSObject().getDescribe().getLocalName()
            				+ '\n relationship name: ' + cr.getRelationshipName();
	}    
}
System.debug('RELATIONSHIPS: ' + childRelationships);

Good luck!
This was selected as the best answer
병훈 송병훈 송
Thank you, very much! monsterloomis!

I completed this problem!