1

I have a Schema util class like the following one:

public with sharing class ObjectDescribe {
    public static Map<String, Schema.SObjectField> getFieldsMap(sObject obj) {
        DescribeSObjectResult describeObject = obj.getSObjectType().getDescribe();

        return describeObject.fields.getMap();;
    }
}

then to call the getFieldsMap() method in a class:

public with sharing class TestDescribe {
    public static void execute() {
        Opportunity obj = new Opportunity();
        objectFields = ObjectDescribe.getFieldsMap(obj);

        for (String nm : objectFields.keyset()) {
            DescribeFieldResult fr = objectFields.get(nm).getDescribe();
        }
    }
}

The result dependents on the class version:

  • Works well: ObjectDescribe class is v48.0, TestDescribe class is v48.0
  • Got System.SObjectException: ObjectDescribe class is v48.0, TestDescribe class is v35.0

Why does call the Schema.SObjectField.getDescribe() from a low version class that causes the System.SObjectException?

1 Answer 1

3

ContactId field may be added in some higher version of 35.0.

To verify this, make a describe call to workbench's rest explorer on below URLs, below are the results.

  1. /services/data/v48.0/sobjects/Opportunity/describe/

    • ContactId exists in fields in 48.0
  2. /services/data/v35.0/sobjects/Opportunity/describe/

    • ContactId does not exist in fields 35.0

Now after running the example, the error it gives on my DEV org is:

System.SObjectException: Field Opportunity.ContactId is inaccessible in this context


The describe call is made in ObjectDescribe returns the ContactId field as its on 48.0.

But when you try to make a field describe on fields returned on class with 35.0, it fails because the TestDescribe class runs in lower version of class where ContactId field doesn't exists, hence the error.

Note: Usually its a good practice to upgrade the version all your code every few years if not on every release. Version mismatches may lead to hidden errors, this one was relatively easy to verify.

1
  • 1
    Thanks for your information.
    – Jair
    Commented May 26, 2020 at 10:23

You must log in to answer this question.

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