function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hazee.LiHazee.Li 

MiddleName Field cannot find in ".fields.getMap()" inside the method

Hi All,
We've just enabled the Middle Name and Suffix Fields feature and encountered the below issue.

SUMMERY : Just after enabling "Middle Name and Suffix Fields", Implementation of custom apex logic in a static method vs running similar code snippet separately in an anonymous apex gives different result;

Runing as a method : null pointer exception (no middleName field)
Runing as anonymous : works fine!

DESCRIPTION : Below code throws an error while trying to access the middle name field in a static method,
 
private static Map<String,String> compHistoryObjDevNameToFieldName; 
// this will hold field name to label in order to access as a cache
	/**
	 * Returns:   field label of the object
     * field label  name will  return  from  compHistoryObjDevNameToFieldName if the value 
     * is cached in compHistoryObjDevNameToFieldName map it will return the value directly
     * or if  the  value  does  not exsist the logic will  cache the  new value and return
     **/ 
    public static String getFieldLabel(Id objectId, String fieldName) {
        // prepare the key of the map ObjectId prefix+fieldname 
        String recordPrefix = objectId.getSobjectType().getDescribe().getKeyPrefix(); 
        String ObjDevNameKey = recordPrefix + fieldName;
        
        //1. initialse map
        //if the map is null initialize the instance
        if (compHistoryObjDevNameToFieldName == null) {
            compHistoryObjDevNameToFieldName = new Map<String,String>();
        }

        //if the key is not contains add the nmew field to the map
        if (!compHistoryObjDevNameToFieldName.containsKey(ObjDevNameKey)) {
			Schema.DescribeSobjectResult objDesc = objectId.getSobjectType().getDescribe();
			compHistoryObjDevNameToFieldName.put(ObjDevNameKey, 
                                    objDesc.fields.getMap().get(fieldName).getDescribe().getLabel());
        }

        // return the field label
        return compHistoryObjDevNameToFieldName.get(ObjDevNameKey);
    }

However while debugging this issue, we found that if we access the fields.getMap() directly in an
anonymous execution, this gives the expected result.


Please refer the code executed in an anonymous window;
 
//Id objectId = '0011900000ApDykAAF'; // Person Account
//Id objectId = '0011900000BvwU6'; // Organisation
Id objectId = '0031900000AvXtg'; // Contact

// Accessing the method which contains the similar logic
try{
    // System.NullPointerException: Attempt to de-reference a null object at this point
    system.debug('Result::' + LibUtil.getFieldLabel(objectId, 'MiddleName'));
} catch(Exception e) {
    system.debug('Exception::' + e);
}

// implementing the similar logic(same set of code contains inside the method 
// LibUtil.getFieldLabel()) in anonymous window
Schema.DescribeSobjectResult objDesc = objectId.getSobjectType().getDescribe();
Map<String,Schema.SObjectField> fieldMapForLabel = objDesc.fields.getMap();
for(String key : fieldMapForLabel.keySet()){
    system.debug('DEBUG :: '+objDesc.fields.getMap().get(key));
}

Thanks in advance
 
Best Answer chosen by Hazee.Li
Andy BoettcherAndy Boettcher
The first thing I would do to set the API version of your class to the latest and greatest (v35) and see if that remedies the issue.  Odds are your class is at an API level that does not support this new functionality.

All Answers

Andy BoettcherAndy Boettcher
The first thing I would do to set the API version of your class to the latest and greatest (v35) and see if that remedies the issue.  Odds are your class is at an API level that does not support this new functionality.
This was selected as the best answer
Hazee.LiHazee.Li
Thank you so much Andy!
Ankita PatelAnkita Patel
Thanks, Andy !!
ur suggestion solved my problem related to NULL pointer Exception issue.
I was getting Error while running test class and it was getting fail but after changing its API version it was successfully executed.
but I didn't understand how ?
can u explain ?