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
BarryPlumBarryPlum 

Help with Maps and Sets

I have a custom object that can be related to either a Lead or a Contact.  I want to search both objects by the email address in the custom object.

I know I should probably create a map, but I'm having difficulty getting to the point where I can actually make the trigger work.

Here is where I started, create a set with all the emails from the trigger:

Code:
SET<String> emails = new SET<String>();
for (Registration__c r : Trigger.new) {
 if (r.email__c != null) {
  emails.add(r.email__c);
 }
}

Now, I want to search for matches from Leads and contacts based on that set... Here's what I thought I would do:
Code:
Map<ID, Contact> conMap = new Map<ID, Contact>([select id,email from Contact where email IN :emails]);
Map<ID, Lead> leadMap = new Map<ID, Lead>([select id, email from Lead where email IN :emails]);

Still think this may work, but the problem is, how do I either get that into a single <String,ID> map where I can iterate through the trigger and update the relationship fields or search each map individually by the email in the trigger to get the ID for the relationship fields.

Thanks in advance for the advice.

 
 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef
ok try this;
Code:
for (Registration__c r : Trigger.new) {
 String email = r.Email__c;
 if (conMap.containsKey(email.toLowerCase)) {
  system.debug('found contact');
  r.Contact__c = conMap.get(email.toLowerCase);
  system.debug('updated contact');
 } else if (leadMap.containsKey(email.toLowerCase)) {
  system.debug('found lead');
  r.Lead__c = leadMap.get(email.toLowerCase);
  system.debug('updated lead');
 } 
} 
}

 and if it gives you a casting error do this:
Code:
for (Registration__c r : Trigger.new) {
 String email = '' + r.Email__c;
 if (conMap.containsKey(email.toLowerCase)) {
  system.debug('found contact');
  r.Contact__c = conMap.get(email.toLowerCase);
  system.debug('updated contact');
 } else if (leadMap.containsKey(email.toLowerCase)) {
  system.debug('found lead');
  r.Lead__c = leadMap.get(email.toLowerCase);
  system.debug('updated lead');
 } 
} 
}

 


All Answers

mikefmikef
BarryPlum:

have you trigger look like this:
Code:
SET<String> emails = new SET<String>();
for (Registration__c r : Trigger.new) {
 if (r.email__c != null) {
  emails.add(r.email__c);
 }
}

Map<String,Id> conMap = new Map<String,Id>();
Map<String,Id> leadMap = new Map<String,Id>();

for(Contact con : [select id,email from Contact where email IN :emails]){
   conMap.put(con.Email,con.Id);
}
for(Lead l : [select id, email from Lead where email IN :emails]){
   leadMap.put(l.Email,l.Id);
}

for (Registration__c r : Trigger.new) {
   if(conMap.containsKey(r.Email)){
      r.Contact__c = conMap.get(r.Email);
   }

   if(leadMap.containsKey(r.Email){
      r.Lead__c = leadMap.get(r.Email);
   }
}

I am assuming that you have two lookup relationships on Registration, call Contact__c and Lead__c please change these fields to the correct name.
 



Message Edited by mikef on 10-08-2008 09:45 AM
BarryPlumBarryPlum
That's awesome... so close.  I was actually barking up that tree right before I posted.  Everything saves ok in the sandbox, but it doesn't appear to fall into the loop.  Any Ideas?

Here's the code with debug:
Code:
trigger regPeopleMatch on Registration__c (before insert, before update) {
SET<String> emails = new SET<String>();
for (Registration__c r : Trigger.new) {
 if (r.email__c != null) {
  emails.add(r.email__c);
 }
}

Map<String,ID> conMap = new Map<String,ID>();
Map<String,ID> leadMap = new Map<String,ID>();

for(Contact con : [select id,email from Contact where email IN :emails]){
   conMap.put(con.Email,con.Id);
}
for(Lead l : [select id, email from Lead where email IN :emails]){
   leadMap.put(l.Email,l.Id);
}
system.debug(conMap);
system.debug(leadMap);

for (Registration__c r : Trigger.new) {
 if (conMap.containsKey(r.email__c)) {
  system.debug('found contact');
  r.Contact__c = conMap.get(r.email__c);
  system.debug('updated contact');
 } else if (leadMap.containsKey(r.email__c)) {
  system.debug('found lead');
  r.Lead__c = leadMap.get(r.email__c);
  system.debug('updated lead');
 } 
} 
}

 
Here is the debug output:
20081008171807.519:Trigger.regPeopleMatch: line 7, column 1: SelectLoop:LIST:SOBJECT:Registration__c
20081008171807.519:Trigger.regPeopleMatch: line 16, column 1: SelectLoop:LIST:SOBJECT:Contact
20081008171807.519:Trigger.regPeopleMatch: line 16, column 19: SOQL query with 1 row finished in 7 ms
20081008171807.519:Trigger.regPeopleMatch: line 19, column 1: SelectLoop:LIST:SOBJECT:Lead
20081008171807.519:Trigger.regPeopleMatch: line 19, column 14: SOQL query with 0 rows finished in 5 ms
20081008171807.519:Trigger.regPeopleMatch: line 22, column 1: {xxx@xxxx.com=0036000000nf1IvAAI}
20081008171807.519:Trigger.regPeopleMatch: line 23, column 1: {}
20081008171807.519:Trigger.regPeopleMatch: line 27, column 1: SelectLoop:LIST:SOBJECT:Registration__c


BarryPlumBarryPlum
OK... so figured out the problem is capitalization, which I should have accounted for anyway. 

When the email gets put into the map, it's put in as lower case (or maybe in the case on the lead or contact record), however the containsKey method does NOT ignore case.  So, how do I get the containsKey method to ignore case.  I tried using r.email__c.toLowerCase and it thought it was a relationship...

Since we're doing string comparisons, I should really be consistant across the board with case.
mikefmikef
ok try this;
Code:
for (Registration__c r : Trigger.new) {
 String email = r.Email__c;
 if (conMap.containsKey(email.toLowerCase)) {
  system.debug('found contact');
  r.Contact__c = conMap.get(email.toLowerCase);
  system.debug('updated contact');
 } else if (leadMap.containsKey(email.toLowerCase)) {
  system.debug('found lead');
  r.Lead__c = leadMap.get(email.toLowerCase);
  system.debug('updated lead');
 } 
} 
}

 and if it gives you a casting error do this:
Code:
for (Registration__c r : Trigger.new) {
 String email = '' + r.Email__c;
 if (conMap.containsKey(email.toLowerCase)) {
  system.debug('found contact');
  r.Contact__c = conMap.get(email.toLowerCase);
  system.debug('updated contact');
 } else if (leadMap.containsKey(email.toLowerCase)) {
  system.debug('found lead');
  r.Lead__c = leadMap.get(email.toLowerCase);
  system.debug('updated lead');
 } 
} 
}

 


This was selected as the best answer