You need to sign in to do that
Don't have an account?
Hazee.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,
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;
Thanks in advance
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
All Answers
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 ?