You need to sign in to do that
Don't have an account?
Tyler Harris
Trying to find a duplicate lead based on email on a "before insert" trigger. I pushed a SOQL query results into a map. How do I find/compare the duplicates in the Map?
trigger LoveMessage on Lead (before insert) {
List<Lead> newleads = new List<Lead>();
List<Lead> currentleads = new list<Lead>();
currentleads = [SELECT Id, Name, Email FROM Lead WHERE Email !=null];
Map<String, String> compareleads = new Map<String, String>();
for(Lead u: currentleads){
compareleads.put(u.Name, u.Email);
System.debug(compareleads);
}
for(Lead lovelylead:Trigger.new){
if(lovelylead.Email != null && lovelylead.Email !=compareleads.Email){
newleads.add(lovelylead);
}
}
insert newleads;
}
Trying to find duplicate leads in Trigger
Trying to find a duplicate lead based on email on a "before insert" trigger. I pushed a SOQL query results into a map. How do I find/compare the duplicates in the Map?
trigger LoveMessage on Lead (before insert) {
List<Lead> newleads = new List<Lead>();
List<Lead> currentleads = new list<Lead>();
currentleads = [SELECT Id, Name, Email FROM Lead WHERE Email !=null];
Map<String, String> compareleads = new Map<String, String>();
for(Lead u: currentleads){
compareleads.put(u.Name, u.Email);
System.debug(compareleads);
}
for(Lead lovelylead:Trigger.new){
if(lovelylead.Email != null && lovelylead.Email !=compareleads.Email){
newleads.add(lovelylead);
}
}
insert newleads;
}
Note that if there are any duplicates in the list of new leads, this solution will only return the first one.
All Answers
Note that if there are any duplicates in the list of new leads, this solution will only return the first one.
Also, I"m trying to "bulkify" the trigger. Is the code setup to satisfy this requirement? Why or why not?
Thank you so much.
Apex script unhandled trigger exception by user/organization: 005F000000496Il/00DF0000000gseU
LoveMessage: execution of BeforeInsert
caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old
Trigger.LoveMessage: line 14, column 1
If you wanted to retain all three leads for some reason, you could use an if statement like this: if (!compareleads.containsKey(lovelylead.Email)) -- this way, your compareleads map never changes, and as long as your database doesn't contain any leads with that email address, any number of duplicates can get added.
The example is indeed bulkified. All this means is that you can call the trigger with a collection of leads, and any SOQL queries or DML statments get executed only once per transaction (rather than once per lead in your collection). The query in your example only executes once, and the insert statemen also only once.
However, and I apologize for missing this earlier, since this is a before insert trigger, you can't call insert again from inside your trigger. This is why you are getting that DML exception. What you want to do instead is add an error to any leads you don't want to get inserted: badlead.addError('Lead with this email address already exists.'); the gotcha with this is that it will also prevent any good leads from being inserted by default, so in the code that inserts the leads, you'll want to use database.insert(listOfLeads, false) to indicate that partial-success DML is allowed.
trigger LoveMessage on Lead (before insert) {
List<Lead> newleads = New List<Lead>();
List<Lead> currentleads = new List<Lead>();
List<Lead> duplicates = new List<Lead>();
currentleads = [SELECT Id, Name, Email FROM Lead WHERE Email !=null];
Map<String, String> compareleads = new Map<String, String>{};
for(Lead u: currentleads){
compareleads.put(u.Name, u.Email);
}
for(Lead lovelylead:Trigger.new){
if(lovelylead.Email != null && (compareleads.put(lovelylead.email, lovelylead.name)==null)){
newleads.add(lovelylead);
}
else{
duplicates.add(lovelylead);
duplicates.addError('This is a duplicate');
}
}
}