Map<String, Schema.fieldset> fieldMap = Schema.SObjectType.Object_Name.fieldSets.getMap();  

this properly returns a map of the fieldsets paired with their API names but the names come in all lowercase - everything else maintains its casing.

Is this default behavior and, if so, is there a workaround to get the proper casing?

up vote 5 down vote accepted

Yes, this is the default behavior, and no there's no "direct" workaround. The reason why is because the Map returned by the call is actually a special map that I'll call CaseInsensitiveKeyMap. A normal map would more appropriately be called a CaseSensitiveKeyMap, because "Hello" and "hello" are two unique keys (because they use hashCode(), which will definitely return unique values for even a change in string case).

On the other hand, the describe calls, such as fields.getMap() and fieldSets.getMap(), effectively call String.toLowerCase() on each key before calling hashCode(), such that you can ask for Name, NAME, or name (or any other permutation) to get the correct field. Field names are case insensitive in Apex Code, and this map mimics that behavior.

If you absolutely wanted to have a case sensitive map, you'd have to construct your own Map, iterate through the values from the describe call, and set each key individually. This would be a large waste of CPU time for no real benefit, since there shouldn't be any specific reason why you need to have the correct casing.

Yes it is the default behavior.

I usually loop on keyset and get description of the field with getDescribe

Map<String,Schema.SObjectField> fieldsMap = objectDescribe.fields.getMap(); 
for(SObjectField field:fieldsMap.values()){
    String name = field.getDescribe().getName();
  • 2
    You could just iterate through values() instead, though; this is faster since you won't have to retrieve the values from the map. – sfdcfox Sep 25 '14 at 15:40
  • improved @sfdcfox – Akram G Sep 25 '14 at 15:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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