You need to sign in to do that
Don't have an account?
BarryPlum
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:
Now, I want to search for matches from Leads and contacts based on that set... Here's what I thought I would do:
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.
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.
and if it gives you a casting error do this:
All Answers
have you trigger look like this:
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
Here's the code with debug:
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.
and if it gives you a casting error do this: