1

I need to find all lookup relationships to the Contact object across all of our Objects in Salesforce. So any object which has a lookup field to the Contact object including the standard fields (e.g. Case). I think that the Metadata API might be a good approach for this but I haven't ran anything similar in the past.

Anyone completed this before and have any suggestions? Thanks in advance!

3
  • Does this need to be programmatic or could you simply look at the related lists for Contact in the Setup UI?
    – Phil W
    Jan 10, 2023 at 21:44
  • I think it would have to be programmatic - we have something like 1400 objects when accounting for managed packages and custom.
    – cg13
    Jan 10, 2023 at 21:50
  • You must be on unlimited edition then. Does the Setup UI not list all related lists for Contact then?
    – Phil W
    Jan 10, 2023 at 22:22

1 Answer 1

5

In Apex Code, you can just check the ChildRelationship members:

for(ChildRelationship relationship: sObjectType.Contact.getChildRelationships()) {
    System.debug(relationship.getChildSObject()+'.'+relationship.getField());
}

In the REST API, you're given the list of child relationships when you describe the parent object:

/services/data/v56.0/sobjects/Contact/describe

Example output:

Example Describe Output for the Contact sObjectType

You can also call the same thing in the SOAP API (describeSObjects(['Contact'])), and the in the UI-API /services/data/v56.0/ui-api/object-info/Contact.

The Metadata API is, perhaps ironically, one of the worst APIs to try and discover all relationships; it's only meant to be used to manipulate custom metadata (not just Custom Metadata itself, but any kind of custom metadata, like fields, validation rules, etc). Using any of the describe calls is typically better for metadata discovery.

1
  • That is somewhat ironic - thank you for this - very helpful!
    – cg13
    Jan 11, 2023 at 14:56

You must log in to answer this question.

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