I have an application which allows the user to specify a geography object and field. For example

User's country field

Account's State Field

Now for Leads the system needs to dynamically determine its relationship to the specified object and parse the field.

eg: When we select User object as the Geography object and country field, the system The system should automatically determine the relationship i.e Owner and fetch the relationship Lead.Owner.country In case of account, it should do Lead.Account.name and so forth. The thing is I don't know the relationship in advance it could be any object So is there a direct SOQL query, or an API I can hit to get relationships between two objects(any sort of relationship)

1 Answer 1


You didn't say whether you were doing this within SFDC or via a 3rd party system

If within SFDC, using Apex, you can do the following:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // map objname to tokens
for (String sobjname : gd.keySet()) {
     Schema.DescribeSObjectResult sobjDescribe = gd.get(sobjname).getDescribe();
     List<Schema.ChildRelationship> crList = sobjDescribe.getChildRelationships();
     for (Schema.ChildRelationship cr : crList) {
        // Do something with child relationships such as save interesting ones in a map
        // Each cr will identify the child SObject's token via cr.getChildObject()
        // From the child SObject's token, you can get the child SObject's name via
        // ... getDescribe().getname() 

If using the above solution and the application must be bulkified (as all should be), the first line to build the global describe should only be done once in the transaction to avoid limits (V30-) / save CPU time (V31+)

If from a third party system, you can reconstruct the same using the REST API, SOAP API or Metadata API

You must log in to answer this question.

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