You need to sign in to do that
Don't have an account?

APEX: Lead Insertion Trigger Duplicate Prevention: Don't roll back, instead merge/update?
I'm having a difficult time finding the answer to this question.
I coded up a before insert trigger that checks both the insertion set (all leads being currently inserted) as well as existing Leads+Contacts to see if the leads trying to be inserted already exist or are duplicated in the insertion set. The check is a cross-check on their email or alt-email for the insertion set itself, Leads, and Contacts.
I prevent the insertion of duplicate leads by adding an error when a duplicate is found (either in the insertion set, or already existing Lead/Contact in Salesforce), but doing this unfortunately rolls back the insert preventing that lead from being inserted and trying to insert again without that lead (as Salesforce does). This does not work for our purposes because ultimately if 2 leads insert at the same time (in the same insertion set) from our web insert they should merge into 1, not simply stop the second insert.
Instead of removing the duplicated lead from being inserted entirely, in cases where the existing Lead/Contact can be updated, or 2 leads are being inserted in this insertion set that are duplicated and can be merged I would like to update / merge the Leads, but I can't do that if it rolls back the insertion of all leads when I error the insert of 1 lead.
In short:
How can I prevent insertion of a single Lead in my insertion set without it rolling back and restarting the trigger (removing the lead from the insertion set and running the insert again) so that in the trigger itself I can either A: merge the lead with the other lead in the insertion set which is a duplicate, or B: update the existing Lead / contact with the new information.
Right now I'm thinking that this isn't possible and instead I should allow the lead to insert but set some sort of flag on it that says "Duplicate of LEAD_ID / CONTACT_ID" and then run a trigger after insert that merges the duplicated lead with the existing/duplicated-in-set lead/contact. Would that work? Is there a cleaner way of doing this? What am I not thinking about / should I keep in mind with this insertion logic?
Any assistance here is much appreciated. Thanks!
I coded up a before insert trigger that checks both the insertion set (all leads being currently inserted) as well as existing Leads+Contacts to see if the leads trying to be inserted already exist or are duplicated in the insertion set. The check is a cross-check on their email or alt-email for the insertion set itself, Leads, and Contacts.
I prevent the insertion of duplicate leads by adding an error when a duplicate is found (either in the insertion set, or already existing Lead/Contact in Salesforce), but doing this unfortunately rolls back the insert preventing that lead from being inserted and trying to insert again without that lead (as Salesforce does). This does not work for our purposes because ultimately if 2 leads insert at the same time (in the same insertion set) from our web insert they should merge into 1, not simply stop the second insert.
Instead of removing the duplicated lead from being inserted entirely, in cases where the existing Lead/Contact can be updated, or 2 leads are being inserted in this insertion set that are duplicated and can be merged I would like to update / merge the Leads, but I can't do that if it rolls back the insertion of all leads when I error the insert of 1 lead.
In short:
How can I prevent insertion of a single Lead in my insertion set without it rolling back and restarting the trigger (removing the lead from the insertion set and running the insert again) so that in the trigger itself I can either A: merge the lead with the other lead in the insertion set which is a duplicate, or B: update the existing Lead / contact with the new information.
Right now I'm thinking that this isn't possible and instead I should allow the lead to insert but set some sort of flag on it that says "Duplicate of LEAD_ID / CONTACT_ID" and then run a trigger after insert that merges the duplicated lead with the existing/duplicated-in-set lead/contact. Would that work? Is there a cleaner way of doing this? What am I not thinking about / should I keep in mind with this insertion logic?
Any assistance here is much appreciated. Thanks!
All Answers
set<lead> yourinitialset = (whateveryouhavehere)
set<lead> leadsInsert = new set<lead>();
set<lead> leadsFailed = new set<lead>();
for checking 2 in the same set duplicated (far as i know sets don't admit dupplicates by themselves), iterate the "leadsinsert" set when running trought your initial set.
for(lead ld : yourinitialset)
if(leadsinsert.size() == 0){
leadsinsert.add(ld);
}else{
for(lead lead : leadsinert){
if(lead.email == ld.email){
/* merge the fields */
} else {
/* check your leads and add to your insert set or to your "fail" set */
}
}
}
if(leadsinsert.size() > 0){
insert leadsinsert;
}
something like that :D
try to adapt it to your case and sorry for just write it right here without testing.
-Great, so you can check if there is another Lead created already.
The leads are going to insert if I don't do anything,
-Yep
I don't code that part, it's already happening.
-You can code the changes for doing the new validations?
The problem is that if I prevent 1 lead from inserting it rolls back all changes
-Don't throw an error, just add the lead to a "failed list" (like List<lead> FailedInsertion = new List<Lead>(); ) and handle the elements on the list.
And starts over again preventing me from running any additional logic involving the duplicate.
-This should be fixed with the above point.
I'm sorry but the last post just doesn't make any sense at all in this context.
-It does, before you insert your lead, query leads that has the same "X" field that your new one has. But we can't know if would work for you because you haven't post your code or part of it.
Maybe you can try this: https://developer.salesforce.com/page/Apex_Day_Lab_Exercises#Exercise_2%3aLead_duplicate_prevention
If that doesn't help you, I don't know what more to tell you.
Good Luck.