Inspector false positive when referencing fields named 'SobjectType'

Issue #1737 resolved
Eric Kintzer created an issue

SFDC has a few SObjects (QueueSObject, RecordType, …) where there is a field on the object called “SObjectType”. The inspector gets confused, thinking I’ve coded a reference to an object of type SobjectType when in fact, I’m referring to a field named SobjectType on an SObject.

SObjectField fld = QueueSobject.SobjectType;

SobjectField[] flds = new List<SObjectField> {QueueSobject.SObjectType};

Compiles and runs fine.

Comments (4)

  1. Scott Wells repo owner

    There's something very weird going on here with the SobjectType fields on some of these standard SObjects. It actually seems that there's a class variable named SObjectType that's of type Schema.SObjectField and there's also an instance variable named SobjectType that's of type String. For example, the following compiles just fine:

    // Class variable usage as SObjectField
    System.debug(ListView.SobjectType.getDescribe());
    
    // Instance variable usage as String
    List<ListView> listViews = [SELECT Id, Name, SobjectType FROM ListView];
    for (ListView listView : listViews)
    {
        System.debug(listView.SobjectType.toUpperCase());
    }
    

    I'm not going to deliver any changes here until I figure out what's going on. It seems to me that there's something happening that's otherwise unsupported in the Apex language.

  2. Scott Wells repo owner

    I spoke with some key folks at Salesforce about this. It's a known bizarre behavior. I'm not 100% sure what I'm going to do about it in IC2 given how non-standard it is. I'm hesitant to implement too many workarounds, so I may just suppress the illegal assignment code inspector in that specific instance. It won't fix things like chained code completions and such, but it would at least quiet down the noise.

  3. Scott Wells repo owner

    Change committed to ignore type mismatch for SobjectType fields when assigned to SObjectField if they aren't of type SObjectType. This is actually just an extension of an existing inspection exception rule that was previously not applied to the SObjectType field but now is selectively. This will be included in the next build.

  4. Log in to comment