2

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));
    }

QUESTION: why is the MiddleName field is not available when access the field from a method with .fields.getMap().get('MiddleName').getDescribe().getLabel()

Thanks in advance

2
  • Your class is 'with sharing'? – Santanu Halder Nov 18 '15 at 7:28
  • No it isn't with sharing! – Hasantha Nov 18 '15 at 10:50
0

Finally I found an answer for my question, what I only had to do is to change the version of the API to 35 :-|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.