1

I have a method that returns a map of all the fields where the field's API name is the key and the object Schema.SObjectField is the value.

    private static Map<String, Schema.SObjectField> fieldMap(String objectType){
         Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
         Schema.SObjectType objectSchema = schemaMap.get(objectType);
         Map<String, Schema.SObjectField> fieldMap = objectSchema?.getDescribe().fields.getMap();
         return fieldMap;
    }

It works well with standard objects but it won't with custom objects. The line Map<String, Schema.SObjectField> fieldMap = objectSchema?.getDescribe().fields.getMap(); throws the error Attempt to de-reference a null object.

Any idea why? The Object's API Name is CO_Shuttle__c.

Thank you,

1
  • Does this happen in the org that you initially created the custom SObject in? Or is it happening in an org that you deployed this custom SObject to?
    – Derek F
    Commented Aug 14, 2022 at 22:14

1 Answer 1

2

Is your object part of a managed package? If so, you may need to deal with the namespace as well.

Map<String, sObjectField> mapping = fieldMap('myNs__CO_Shuttle__c');

You can avoid a lot of this hassle by using an sObjectType token if you have it:

static Map<String, Schema.SObjectField> fieldMap(sObjectType token) {
  return token?.getDescribe().fields.getMap();
}

But at that point, it's a one-liner; you can just use this directly.

Map<String, sObjectField> fieldMap = sObjectType.CO_Shuttle__c.fields.getMap();

Note that using a token guarantees you will find the sObjectType or you'll get a compilation error. Always prefer to use hard-coded tokens whenever possible.

If you absolutely need it to be dynamic, you can also use Type.forName:

static Map<String, sObjectField> fieldMap(String objectApiName);
  Type sType = Type.forName('Schema', objectApiName);
  if(sType == null) {
    throw new IllegalArgumentException('Unable to find '+objectApiName);
  }
  sObject template = (sObject)sType.newInstance();
  sObjectType sType = template.getSobjectType();
  return sType.getDescribe().fields.getMap();
}

I would strongly advise against using your method as written, as Schema.getGlobalDescribe() is very expensive in CPU time, and may even time out in large orgs.

6
  • Thanks a lot! I The object isn't part of a managed package, and I would need it to be dynamic indeed... I tried the method with Type.forName but I get the following error IllegalArgumentException:[]: Unable to find CO_Shuttle_c. The problem seems to be that Schema can't access custom objects for some reason...
    – Jean
    Commented Aug 14, 2022 at 22:26
  • 1
    Your previous comment suggests a typo. It would end "__c" not "_c".
    – Phil W
    Commented Aug 14, 2022 at 22:32
  • If the object is not part of a managed package, why does your question show myNs.CO_Shuttle__c (I.e. with an attempt to add a namespace prefix)? If it needs a namespace, the correct form is myNs__CO_Shuttle__c.
    – Phil W
    Commented Aug 14, 2022 at 22:35
  • @PhilW Haha, yeah, sorry. I'll edit that.
    – sfdcfox
    Commented Aug 14, 2022 at 22:36
  • @Jean As Phil mentioned, looks like you have a typo: it's __c, not _c. Very common mistake to make if you're not paying attention or new to Salesforce. We've all done it at least once. 😅
    – sfdcfox
    Commented Aug 14, 2022 at 22:40

You must log in to answer this question.

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