function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
paul.mcgurnpaul.mcgurn 

Using an email address as the Key for a Map

This seems to be bugged.

Example

Map<string,Contact> ccMap = new Map<string,Contact>();

Contact c1 = new Contact();
c1.firstname = 'Joe';
c1.lastname = 'User';
c1.email = 'joe@example.com';
c1.accountId ='<some id>';

ccMap.put(c1.email,c1);

This will always retgurn null:
Contact con = ccMap.get(c1.email);

I didn't see anything in the Apex documentation noting restrications on the string's value when creating a custom map.
MissedCallMissedCall
Map<string,Contact> ccMap = new Map<string,Contact>();

Contact c1 = new Contact();
c1.firstname = 'Joe';
c1.lastname = 'User';
c1.email = 'joe@example.com';
//c1.accountId ='<some id>';

ccMap.put(c1.email,c1);
system.debug('------ '+ccMap.get(c1.email));
debug log:
Anonymous execution was successful.

30.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: Map<string,Contact> ccMap = new Map<string,Contact>();
Execute Anonymous: 
Execute Anonymous: Contact c1 = new Contact();
Execute Anonymous: c1.firstname = 'Joe';
Execute Anonymous: c1.lastname = 'User';
Execute Anonymous: c1.email = 'joe@example.com';
Execute Anonymous: //c1.accountId ='<some id>';
Execute Anonymous: 
Execute Anonymous: ccMap.put(c1.email,c1);
Execute Anonymous: system.debug('------ '+ccMap.get(c1.email));
14:39:35.033 (33632509)|EXECUTION_STARTED
14:39:35.033 (33646391)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
14:39:35.034 (34645986)|USER_DEBUG|[10]|DEBUG|------ Contact:{Email=joe@example.com, FirstName=Joe, LastName=User}
14:39:35.063 (34691763)|CUMULATIVE_LIMIT_USAGE
14:39:35.063|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

14:39:35.063|CUMULATIVE_LIMIT_USAGE_END
What's your question?

 
paul.mcgurnpaul.mcgurn
This wasn’t working for me. I ended up having to log out of Salesforce and log back in (I use the web based dev console), and then it did. So it was ‘some’ bug, but not specifically this.