+ Start a Discussion
Niklas HillgrenNiklas Hillgren 

Map<String, SobjectField> bugg/feature?

SObjectType objToken = Contact.sobjectType;
Map<String, SObjectField> withPrefix = objToken.getDescribe().fields.getMap();
//check rule is fully populated

Map<String, SObjectField> fields = new Map<String, SObjectField>();
//Create a new map without prefix
for( String key : withPrefix.keySet() ){
    fields.put(key, withPrefix.get(key));
}

System.debug('WITH PREFIX: ' + withPrefix.get('lastname'));
System.debug('WITH PREFIX 2: ' + withPrefix.get('LastName'));
System.debug('WITH PREFIX 2: ' + withPrefix.get('LastNAmE')); //ETC?!

System.debug('WITHOUT PREFIX: ' +fields.get('lastname'));
System.debug('WITHOUT PREFIX 2: ' +fields.get('LastName'));
 

My output to debug is:
WITH PREFIX: LastName
WITH PREFIX 2: LastName
WITH PREFIX 3: LastName
WITHOUT PREFIX: LastName
WITHOUT PREFIX 2: null

How come can it be that the map<String, SobjectField> withPrefix isn't case sensetive????? I cannot understand it

Can anyone answer this?

 

Vishal Negandhi 16Vishal Negandhi 16
Hello Niklas, 

If you go through Salesforce documentation here for field tokens:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_field_tokens.htm

They have stated :
The map has the following characteristics:
It is dynamic, that is, it is generated at runtime on the fields for that sObject.
All field names are case insensitive.
The keys use namespaces as required.
The keys reflect whether the field is a custom object.
Niklas HillgrenNiklas Hillgren

I still question the reason for doing it this way isntead of having another type of class or some class that extends String. When I'm comparing Strings in a map I would expect that they match the key on hashcode values and act the same no matter of how I instanciates/create the map. 

Does anyone know if it is possible (in APEX) to create a case insensitive map e.g. Map<IgnoreCaseString, SobjectField> without creating my own class?

Niklas HillgrenNiklas Hillgren

Thanks for your answer Vishal.

When I read about the get function in the Map class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm#apex_System_Map_get) it states:

Signature: public Object get(Object key)
Parameters: key Type: Object Return Value Type: Object
Usage: If the key is a string, the key value is case-sensitive.

This is a bit confusing..

Cheers,
Niklas