IDE cannot resolve symbol from custom metadata type relationship field to entity definition

Issue #2114 resolved
Renato Oliveira created an issue

Error: Cannot resolve symbol 'SObjectDefinitionRef__r'

It shows up where that custom field field (“SObjectDefinitionRef__c”) is referenced in the code or in any queries.

Steps to reproduce:

  1. Create a custom metadata type in the org;
  2. Create a custom field of type “metadata lookup” of type “entity definition”, related to a standard/custom object (any);
  3. Try to reference it in an Apex class either within a query or accessing its property.

IntelliJ IDEA 2022.1.1 (Community Edition)
Build #IC-221.5591.52, built on May 10, 2022
Runtime version: 11.0.14.1+1-b2043.45 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.17.5-76051705-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 1948M
Cores: 8
Non-Bundled Plugins:
com.arcticicestudio.nord.jetbrains (0.13.0)
com.illuminatedcloud2.intellij (2.2.2.1)

Kotlin: 221-1.6.21-release-337-IJ5591.52
Current Desktop: pop:GNOME

Comments (17)

  1. Scott Wells repo owner

    Can you provide a concrete code example of the issue? I created a custom metadata type as described and a single custom metadata row against it as follows:

    objects/RelatedToSObject__mdt/RelatedToSObject__mdt.object-meta.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
        <label>RelatedToSObject</label>
        <pluralLabel>RelatedToSObjects</pluralLabel>
        <visibility>Public</visibility>
    </CustomObject>
    

    objects/RelatedToSObject__mdt/fields/ObjectRelationship__c.field-meta.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
        <fullName>ObjectRelationship__c</fullName>
        <externalId>false</externalId>
        <fieldManageability>DeveloperControlled</fieldManageability>
        <label>Object Relationship</label>
        <referenceTo>EntityDefinition</referenceTo>
        <relationshipLabel>RelatedToSObjects</relationshipLabel>
        <relationshipName>RelatedToSObjects</relationshipName>
        <required>false</required>
        <type>MetadataRelationship</type>
        <unique>false</unique>
    </CustomField>
    

    customMetadata/RelatedToSObject.ShoppingList.md

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <label>Shopping List</label>
        <protected>false</protected>
        <values>
            <field>ObjectRelationship__c</field>
            <value xsi:type="xsd:string">ShoppingList__c</value>
        </values>
    </CustomMetadata>
    

    and the editor features (code completion, reference navigation, etc.) work fine, as do queries, e.g.:

    Issue_2114.png

    If there's some other aspect of this functionality that you're referencing, please provide a concrete example. If instead you're not seeing things working as described above in your local install, I'll likely need to get some debug logs from you.

  2. Renato Oliveira reporter

    When the class is opened only the two locations where the reference is used are highlighted by the IDE (in red):

    It saying that cannot resolve:

    I created a new class to test this again and noticed that I can access the ID field (ending in “__c“) but when accessing the relationship (with “__r”) it does not work:

    When the local file is saved the class is submitted to the org successfully too.

  3. Scott Wells repo owner

    Okay. Can you make sure that 1) you're on the absolute latest build of IC2 (though this hasn't changed in a while); and 2) in your connection properties Ensure field read access is enabled, though be sure you understand the behavior of that option before doing so. Assuming you're good with that and you had to do one of these two things (upgrade or enable that option), please regenerate your OST again and see if SObjectDefinitionRef__r is not properly reflected in the resulting SDK.

  4. Scott Wells repo owner

    For what it's worth, here's the OST entry for the CMDT included earlier:

    // Generated by Illuminated Cloud on Mon May 23 14:29:04 CDT 2022. Do not edit.
    
    /**
     * <h3><b>RelatedToSObject</b></h3>
     */
    global class /*Schema.*/RelatedToSObject__mdt extends SObject 
    {
        global String DeveloperName;
        global static final SObjectTypeFields fields;
        global String Label;
        global String Language;
        global String MasterLabel;
        global String NamespacePrefix;
        /**
         * <h3><b>Object Relationship</b></h3>
         * <h3><b>Field Attributes</b></h3>
         * <table border="0" valign="top">
         * <tr><td nowrap><b>Data Type:</b></td><td nowrap>Metadata Relationship</td></tr>
         * <tr><td nowrap><b>Required:</b></td><td nowrap>false</td></tr>
         * <tr><td nowrap><b>Unique:</b></td><td nowrap>false</td></tr>
         * <tr><td nowrap><b>External ID:</b></td><td nowrap>false</td></tr>
         * </table>
         */
        global Id ObjectRelationship__c;
        /**
         * <h3><b>Object Relationship</b></h3>
         * <h3><b>Field Attributes</b></h3>
         * <table border="0" valign="top">
         * <tr><td nowrap><b>Data Type:</b></td><td nowrap>Metadata Relationship</td></tr>
         * <tr><td nowrap><b>Required:</b></td><td nowrap>false</td></tr>
         * <tr><td nowrap><b>Unique:</b></td><td nowrap>false</td></tr>
         * <tr><td nowrap><b>External ID:</b></td><td nowrap>false</td></tr>
         * </table>
         */
        global EntityDefinition ObjectRelationship__r;
        global String QualifiedApiName;
        global static final SObjectType SObjectType;
        global UserRecordAccess UserRecordAccess;
    
        global RelatedToSObject__mdt()
        {
        }
    
        global static Map<String, RelatedToSObject__mdt> getAll()
        {
        }
    
        global static RelatedToSObject__mdt getInstance(Id recordId)
        {
        }
    
        global static RelatedToSObject__mdt getInstance(String qualifiedApiNameOrDeveloperName)
        {
        }
    }
    

    As you can see, it includes ObjectRelationship__r as a first-class field that is of type EntityDefinition. If you're not seeing that in your OST after getting everything up-to-date and configured as described, please create a small standalone project with a CMDT that reproduces this behavior for you and attach it here.

  5. Renato Oliveira reporter

    I tried to create a new project from scratch using the IDE and got multiple node-related errors, as explained in #2115. As such I can’t connect to an existing project.

    I can see the file you posted, and the software versions are as defined in my original post (copied from the “help” section on IntelliJ), so I can guarantee this is the latest build (I downloaded it all yesterday!).

    Using this existing project I tried to enable the “ensure field read access option” and then generating the offline symbol table once more (“full”). It seems alright immediately after reloading, then it starts indexing and when it stops I get the same highlight mistake.

    Highlight message shows:

    Inspection info: This inspection reports references that cannot be resolved to corresponding declarations.

  6. Scott Wells repo owner

    Hi. I've responded on #2115. That sounds like either a misconfiguration of the Salesforce CLI that's registered with IC2 or a difference in the environment available to the CLI when it executes, e.g., you've set local environment variables for your shell that aren't available when the IDE is running. Let's get that remedied and then return to this to see if you can create an environment that reproduces this issue in isolation.

  7. Matthias Blom

    To add on to this, I’ve been having the same issue intermittently. I was trying to get a proper reproduction scenario but wasn’t able to do so. I had a CMDT with an Entity Relation that was working correctly. Then I added a Field Relation, controlled by the Entity Relation, which worked correctly too. Then I removed the Field Relation and replaced it with an Entity Particle Relation which produced the error. But after a couple more tries with the Field Relation I got the same issue there. I was not able to pinpoint what exactly caused the issue. I also had ‘Ensure Field Read Access’ enabled at all times and performed multiple Full Refreshes and project reloads between tries. When looking at the source file in the OST when the issue occurred, only the __c Id field was included and not the __r Relation field.

    While I had those issues on a CMDT in the project, Entity & Entity Particle Relations on another CMDT from a namespaced managed packaged were never loaded correctly. I’m not sure if this is because of another issue or if it has the same underlying cause.

    EDIT: WebStorm 2022.1 and Illuminated Cloud 2.2.2.1

  8. Scott Wells repo owner

    Hi, Matthias. I certainly prefer to hear about a repeatable pattern because it's (generally) one I can address. Since I've so far been unable to reproduce it, it's most helpful if one of you can create a standalone project that reproduces the issue consistently, but even if that's not the case, if you can reproduce it locally then we should be able to get a debug log that shows what's going on. Let me know if you can't do the former (standalone reproduction) and I'll provide instructions of gathering debug logs.

  9. Renato Oliveira reporter

    Reinstalled the IDE (like I mentioned in #2115), and opened the project again. Still got the issue. I’ve created a new project folder from scratch, and will try to replicate the issue later. I’ll keep you updated, but for now just know that the issue persists.

    Build info:

    IntelliJ IDEA 2022.1.1 (Community Edition)
    Build #IC-221.5591.52, built on May 10, 2022
    Runtime version: 11.0.14.1+1-b2043.45 amd64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
    Linux 5.17.5-76051705-generic
    GC: G1 Young Generation, G1 Old Generation
    Memory: 1948M
    Cores: 8
    Non-Bundled Plugins:
    com.illuminatedcloud2.intellij (2.2.2.1)

    Kotlin: 221-1.6.21-release-337-IJ5591.52
    Current Desktop: pop:GNOME

  10. Renato Oliveira reporter

    I have created a scratch org and a set up a custom metadata object with two Metadata relationships. In the editor both show up as options in auto-complete, but only the field that references the ID of the entity in the database. The autocomplete does not work for the relationship fields, such as the case when getting the QualifiedApiName:

  11. Scott Wells repo owner

    Whew! Finally reproduced this. It occurs when you don't have the CMDT metadata locally in the project, so a workaround is to retrieve the CMDT metadata into the project (even if only temporarily) and then generate your OST.

    Having said that, now that I've reproduced it I'm working on a fix for tomorrow's build.

  12. Scott Wells repo owner

    Well, I seem to have spoken prematurely. This seems to be happening due to a bug in the Salesforce APIs that are used to get information about SObjects and their fields, specifically for these types of external relationship fields on CMDTs. Those fields are returned by the APIs with the following type information:

    extraTypeInfo = "externallookup"
    soapType = {SoapType@39193} "XSD_STRING"
    type = {FieldType@39195} "STRING"
    

    Unfortunately there's nothing there about the target type of the external lookup. That's why you end up with the __c fields of type Id (inferred from the extraTypeInfo = externallookup) but no corresponding relationship field. And the reason you end up with the proper relationship field when you have the local metadata is that, when available, IC2 uses the more specific type information from those files, e.g.:

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
        ...
        <referenceTo>EntityDefinition</referenceTo>
        ...
        <type>MetadataRelationship</type>
        ...
    </CustomField>
    

    In this specific situation, I might be able to treat this as a polymorphic relationship field pointing at EntityDefinition, FieldDefinition, and EntityParticle since I know those are the three potential target types. That would get rid of the unresolvable reference errors, but it would also incorrectly treat these fields as all three types instead of one specific type.

    I'll do that for now and then follow up with Salesforce to see if they're aware of this API bug.

  13. Log in to comment