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,