9

I'm attempting to use queries (not describe info) to attain which fields look up to a certain object. I'm stuck trying to decode the format of the ReferenceTo field on FieldDefinition.

Sample code:

List<EntityDefinition> allObjects = [SELECT QualifiedApiName, (SELECT ReferenceTo,Label FROM Fields WHERE RelationshipName!=null) FROM EntityDefinition];

for (EntityDefinition thisObj : allObjects) {
    for (FieldDefinition thisField : thisObj.fields) {
        system.debug(thisField.ReferenceTo);
        // i want to check which object this lookup references
    }
}

Debug is returning: 15:16:45:556 USER_DEBUG [6]|DEBUG|common.api.soap.wsdl.RelationshipReferenceTo@d7f7b38

I'm under the impression ReferenceTo should be an array of strings based on the documentation here: https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_fielddefinition.htm. How can I determine which SObject this relationship is referencing?

1
  • 2
    I found you can use the DataType field which will contain text like Lookup(Account) I can parse that string to determine the lookup object but it feels a little janky.
    – Phil B
    Jun 20, 2020 at 21:05

2 Answers 2

5

When I ran the command sfdx force:data:soql:query --query="SELECT QualifiedApiName, (SELECT ReferenceTo,QualifiedApiName FROM Fields WHERE RelationshipName!=null LIMIT 1) FROM EntityDefinition WHERE QualifiedApiName = 'Account' LIMIT 1" --targetusername sandbox --usetoolingapi --json, it gave the below output:

{
  "status": 0,
  "result": {
    "size": 1,
    "totalSize": 1,
    "done": true,
    "queryLocator": null,
    "entityTypeName": "EntityDefinition",
    "records": [
      {
        "attributes": {
          "type": "EntityDefinition",
          "url": "/services/data/v48.0/tooling/sobjects/EntityDefinition/Account"
        },
        "QualifiedApiName": "Account",
        "Fields": {
          "size": 1,
          "totalSize": 1,
          "done": true,
          "queryLocator": null,
          "entityTypeName": "FieldDefinition",
          "records": [
            {
              "attributes": {
                "type": "FieldDefinition"
              },
              "ReferenceTo": {
                "referenceTo": [
                  "Account"
                ]
              },
              "QualifiedApiName": "MasterRecordId"
            }
          ]
        }
      }
    ]
  }
}

We can see that ReferenceTo contains property named referenceTo and it is a string array.

When I run this System.debug(Schema.SObjectType.FieldDefinition.fields.ReferenceTo.getType());, it gives the output COMPLEXVALUE and this is not listed here

Based on above findings, I came up with the below workaround where we can serialize and deserialize FieldDefinition object.

List<EntityDefinition> allObjects = [SELECT QualifiedApiName, (SELECT ReferenceTo,Label,QualifiedApiName FROM Fields WHERE RelationshipName!=null) FROM EntityDefinition WHERE QualifiedApiName = 'Account'];

for (EntityDefinition thisObj : allObjects) {
    for (FieldDefinition thisField : thisObj.fields) {

        String fieldDefinitionJSONStr = JSON.serialize(thisField);
        Map<String,Object> fieldDefinitionJSONMap = (Map<String, Object>)JSON.deserializeUntyped(fieldDefinitionJSONStr);
        Map<String,Object> referenceToJSONMap = (Map<String,Object>)fieldDefinitionJsonMap.get('ReferenceTo');
        List<Object> referenceToList = (List<Object>)referenceToJSONMap.get('referenceTo');

        if(referenceToList != NULL){
            for(Object obj: referenceToList){
                System.debug(thisField.QualifiedApiName + 'points to object: ' + (String)obj);
            }
        }
    }
}
1
  • Thanks, unfortunately this seems like the best answer. Feels like ReferenceTo should really be a supported type. It also seems like the safest, the alternative of parsing the DataType field I can already foresee issues with namespaces.
    – Phil B
    Jun 21, 2020 at 13:21
0

Query the DataType in FieldDefinition object.. it will return output as in below..

ReferenceTo=common.api.soap.wsdl.RelationshipReferenceTo@3615056c, Label=Account, QualifiedApiName=Account__c, DataType=Lookup(Account)

1
  • Thanks, I mentioned this in a comment on my question. My main concern is parsing the DataType field you could run into issues with namespaced object since it appears to just show the object label not the API name
    – Phil B
    Jun 21, 2020 at 13:30

You must log in to answer this question.

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