+ Start a Discussion
JesseAJesseA 

Comparing String to Email or Converting String to Email

This is a simplified version of my issue, if it doesnt make sense I may have to explain complete scenario. But basically I am having problems  where the value I have is a field of type email, and there is a map whose key's are of type string. The map's value are of type string because the map was build from parsing a JSON response to a webservice call. And the JSON parser only has a getText() value, there is nothing specific to pull out an email value. The map returns false if i perform a containsKey on it, even though if I print the keySet out I see it in there.

 

But I am able to query for records like Leads by looking for those where the email field is contained in a set of strings.

 

I wrote a test and it seems to prove my issue:

Lead l = new Lead(lastName = 'last', Company = 'company', email = 'email@emailTest.com', Country = 'US');
insert l;

Map<String, String> emailsAsStringsMap = new Map<String, String>();
emailsAsStringsMap.put('email@emailTest.com', 'test');

system.debug('THIS IS emailsAsStringsMap.keySet(): ' + emailsAsStringsMap.keySet());


for(Lead insertedLead : [SELECT Id, Email FROM Lead WHERE Email IN : emailsAsStringsMap.keySet()]){
	system.debug('THIS IS insertedLead.Email: ' + insertedLead.Email);
	system.debug('THIS IS emailsAsStringsMap.containsKey(insertedLead.Email): ' + emailsAsStringsMap.containsKey(insertedLead.Email));   
}

 

 

This is the output:

19:02:51.329 (329237000)|USER_DEBUG|[7]|DEBUG|THIS IS emailsAsStringsMap.keySet(): {email@emailTest.com}
19:02:51.329 (329848000)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select Id, Email from Lead 
19:02:51.352 (352823000)|SOQL_EXECUTE_END|[10]|Rows:1
19:02:51.353 (353150000)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
19:02:51.353 (353170000)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
19:02:51.353 (353561000)|USER_DEBUG|[11]|DEBUG|THIS IS insertedLead.Email: email@emailtest.com
19:02:51.353 (353694000)|USER_DEBUG|[12]|DEBUG|THIS IS emailsAsStringsMap.containsKey(insertedLead.Email): false

 

See how the email is in the keySet as a String, but the map containsKey value doesn't seem to be able to equate the email field to the string value.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ManjunathManjunath

Hi,

 

According to the debug log

19:02:51.353 (353561000)|USER_DEBUG|[11]|DEBUG|THIS IS insertedLead.Email: email@emailtest.com

While inserting 't' is small letter where as in the map the 'T' is cap. That is why it is giving false.

 

Regards,

 

All Answers

JesseAJesseA
I could avoid this issue if I could cast a String to an Email value like: (Email) stringValue. But that brings an error.
ManjunathManjunath

Hi,

 

According to the debug log

19:02:51.353 (353561000)|USER_DEBUG|[11]|DEBUG|THIS IS insertedLead.Email: email@emailtest.com

While inserting 't' is small letter where as in the map the 'T' is cap. That is why it is giving false.

 

Regards,

 

This was selected as the best answer
JesseAJesseA

Thanks for noticing that. Turns out "Map keys of type String are case-sensitive."  Guess I just have to copy the emails to a string and set them to lower case.