Salesforce Maps | Key Sensitivity in Map Methods

by - 6:31 PM

Are the Salesforce Map keys case sensitive ? 

I was dealing in a scenario where I was checking case-sensitivity of keys in Salesforce Maps. I read similar issues proposed by 'Mr.Appleman' which he explained beautifully.What I learned at the end of the day is that, for most of the cases the Keys are case-sensitive but for some special cases they are not. Lets quickly check this out

Code below clearly explain the case-sensitivity, copy-paste run anonymously to test 

When Maps Keys are not  case sensitive then?

When we use dynamic apex to obtain describe information, this indicate that the lookup are case-sensitive and if you apply system.debug(gd) you will find all the object names are stored in lower case.

Code below explain global describe and asserting case-sensitivity with 'ContainsKey()'
Now asserting case-sensitivity with 'Keyset.Contains()'

So what is most interesting part  here ?

Though theoretically, both globaldesc.Keyset() and globaldesc.ContainsKey() are equivalent but the first is case-sensitive and later is not

If the key is string then

globalDesc.Keyset().Contains(' ' ) - is case-sensitive
globalDesc.ContainsKey(' ' ) - is not case-sensitive

Notice here in the screenshot below - with ContainsKey() my test assertion pass while failes in with KeySet()

What to keep in mind ?

When you create the packages and use dynamic SOQL, make sure you are aware about keys-sensitivity 

You May Also Like