Академический Документы
Профессиональный Документы
Культура Документы
e 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 1/17
SALESFORCE CODING LESSONS FOR THE 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
Example: How to write a deduping trigger
for leads and contacts
O C T O B E R 1 9 , 2 0 1 3
Preface This post is part of the Write Your First Intermediate Trigger series.
Ok guys lets write a trigger that will prevent a user from creating a lead that already exists as a contact!
Well use the lead/contacts email address to detect duplicates.
A brilliant person once told me that even the most complicated code can be broken down into tiny,
individual steps that are easy to implement. Lets see what our steps are:
1. Lead is created or updated
2. Lead has an email address
3. Try to find a matching Contact based on email address (using SOQL!)
4. If a match is found, give the user an error
5. If a match is not found, do nothing
Now, try to match the steps above with the code! Each step should match to at least one line of code.
Step 5 is the exception, but bonus points if you know where itd go otherwise!
trigger FindDupes on Lead (before insert, before update) {
for (Lead myLead : Trigger.new) {
if (myLead.Email != null) {
List<Contact> dupes = [SELECT Id FROM Contact
WHERE Email = :myLead.Email];
if (dupes.size() > 0) {
String errorMessage = 'Duplicate contact found! ';
errorMessage += 'Record ID is ' + dupes[0].Id;
myLead.addError(errorMessage);
}
}
}
BEGINNER TUTORIALS LOGIN TO SFDC99 HOW TO READ CODE ABOUT ME Follow @dvdkliu
for post updates!
6/15/2014 Example: How to write a deduping trigger for leads and contacts Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 2/17
Mithun Sapui
J UNE 9, 2014 @ 12: 12 PM
Hi David
I have a strange issue here . I have written the below deduping trigger based on your webinar. I have
defined the Dupe contact fields as a text field and have a validation rule on it. I am getting the error
while insert/update with a duplicate email id, but the value of the Id field is not getting populated in the
Dupe Contact field. What am I missing here ? (So, I am getting a validation error on the Dupe Contact
field but the field itself is blank)
trigger DetectDupes on Contact (before update, before insert) {
for (Contact l : Trigger.new) {
if (l.Email != null) {
String contactEmail = l.Email;
list dupeContacts = [Select Email from Contact
where Email = :contactEmail];
if (dupeContacts.size() > 0) {
l.Dupe_contact__c = dupeContacts[0].Id;
} else {
l.Dupe_contact__c = null;
}
} else {
l.Dupe_contact__c = null;
}
}
}
}
And in case youve forgotten
How to read code
Where to write code in Salesforce
Login to Sfdc99 to see this in action!
Next post: An extended look at our deduping trigger!
33 Comments
6/15/2014 Example: How to write a deduping trigger for leads and contacts Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 3/17
Mithun
J UNE 11, 2014 @ 7: 20 PM
Thanks so much, David and Viru. I will do it that way.
Davi d Li u
J UNE 10, 2014 @ 6: 46 PM
Great one Viru, very close! If this was an after trigger you d be spot on!
Since it s a before trigger there is an implied update call automatically at
the end of the trigger. It literally means before update !
It s best if you don t add the Dupe_Contact__c field to the layout and simply
have the error message up on the top of the page. Having the field there
doesn t add any value to the user or the admin. But you re right that the
email address won t show on that field if the validation rule catches it!
Repl y
Viru
J UNE 10, 2014 @ 2: 20 AM
Hello Mithun Sapui,
i think the Dupe_contact__c field is empty because in trigger the record is not committed till you
use DML operation like update or insert.
this trigger work fine but when you assign I.Dupe_contact__c = dupeContacts[0].Id itll
assigned but not committed thats why your text field is still empty when record is duplicated.
Am i right David ?
Reply
Reply
6/15/2014 Example: How to write a deduping trigger for leads and contacts Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 4/17
Reply
Davi d Li u
J UNE 2, 2014 @ 7: 58 PM
hahaha awesome, great observation! I cover how to do this in chapter 5!
Repl y
Sanjay
J UNE 2, 2014 @ 2: 10 PM
Hi David.
I think ,it is not a good option to write SOQL inside loop. So I modified it. Let me know ,is it right?
trigger DuplicateTrigger on Lead (before insert , before update) {
List leadEmail= new List();
for(Lead l:Trigger.new){
leadEmail.add(l.Email);
}
Integer dupContact=[select count() from Contact where Email=:leadEmail];
for(Lead l:Trigger.new){
if(l.Email!=null){
if(dupContact>0){
l.addError(Duplicate lead record );
}
}
}
}
Reply
Amitabh Sharma
6/15/2014 Example: How to write a deduping trigger for leads and contacts Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 5/17
Davi d Li u
MAY 6, 2014 @ 10: 59 PM
Well done Amitabh!!! Way to hang in there!
Repl y
MAY 5, 2014 @ 9: 51 AM
Hi David, this is in continuation to my previous reply.
I modified the code with the following to check only when the lead status is not
closed converted as this is the status that every lead gets on conversion.
This has fixed the problem for now.
if(leadforDupe.Email != null && leadforDupe.Status Closed converted )
Reply
Amitabh Sharma
MAY 5, 2014 @ 9: 34 AM
Hi David, thanks for all the help, I have moved till chapter 7 successfully.
However when i revisited the code on chapter 4, this is working fine if i update an existing Lead record.
When I create a new Lead and try to convert it , again the validation is hit :-(. as the converted contact
will have the same email as that of the lead.
thanks in advance.
/*
not lead a new lead create if the contact exists.
Since leads and contacts are not related
*/
trigger SFDC99_Chap4_LeadDedupe on Lead (before insert, before update) {
for(Lead leadforDupe :Trigger.new){
//checking if the lead has an email
6/15/2014 Example: How to write a deduping trigger for leads and contacts Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/10/19/example-how-to-write-a-deduping-trigger-for-leads-and-contacts/ 6/17
if(leadforDupe.Email != null){
//checking the values of the contacts for emails
List dupe = [SELECT Id,LastName,email FROM Contact where email =: leadforDupe.email];
//if email
if(dupe.size() > 0){
leadforDupe.addError(Duplicate value on + dupe[0].id);
}
}
}
}
Reply
Vaishali Singh
APRIL 29, 2014 @ 9: 29 AM
Hi David,
I know i have been bugging you with my questions. But i am really enjoying coding:
another one for you: I have tried a little different not sure if this is right. I have done this for contact as i
am trying on my developer org and all the licences are consumed so instead of using lead i have used
contact.
But it is giving error on the user page when i am trying to deactivate the user:
Review all error messages below to correct your data.
Apex trigger reassignUser caused an unexpected exception, contact your administrator: reassignUser:
execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with
id 0039000000VUI8GAAX; first error: MIXED_DML_OPERATION, DML operation on setup object is not
permitted after you have updated a non-setup object (or vice versa): Contact, original object: User: []:
Trigger.reassignUser: line 25, column 1