Write a trigger on lead to prevent duplicate records based on lead email Whenever the records is inserted Or updated using apex class handler trigger in Salesforce | How to prevent duplicate lead records based on email in apex trigger Salesforce

6,477 views

Hey guys, today in this post we are going to learn about How to Check Duplicate Email on Lead Object in Salesforce.

Real time scenarios:- Write a trigger on lead to prevent duplicate records based on lead email, if a record already created with the same Email, Or record is Updated. The trigger should throw an error.

Files we used in this post example

duplicateEmailCheckLead.apxt Apex Class Trigger It will be fired wheneverΒ New Lead is Created Or Updated
leadHandlerController.apxc Apex Class Controller Apex handler trigger to prevent duplicate records based on lead email.
Standard Object:-Β Lead Trigger to prevent duplicate lead based on email.

Final Output

Apex Trigger to Check Duplicate Email on Lead Object -- w3web.net

Why Should You Schedule Meeting?

🎯 If You Are Facing Any Of These 6 Challenges. Schedule Meeting With Me.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

  • Find the below steps for this post.

Create Apex Trigger

Step 1:- Apex Class Trigger : duplicateEmailCheckLead.apxt

From Developer Console ➑ File ➑ New ➑ Apex Trigger

duplicateEmailCheckLead.apxt [Apex Class Trigger]

  1.    TRIGGER duplicateEmailCheckOnLead ON Lead (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.         IF(TRIGGER.isInsert){  
  5.             leadHandlerController.updateInsertLead(TRIGGER.new);
  6.         }
  7.         IF(TRIGGER.isUpdate){
  8.  
  9.             leadHandlerController.updateInsertLead(TRIGGER.new);
  10.         }
  11.  
  12.     }
  13.     ELSE IF(TRIGGER.isAfter){
  14.          system.debug('I am inside of after method');
  15.     }
  16.  
  17. }

Create Apex Class Controller

Step 2:- Create Apex Class : leadHandlerController.apxc

From Developer Console ➑ File ➑ New ➑ Apex Class

leadHandlerController.apxc [Apex Class Controller]

  1.    public class leadHandlerController {
  2.  
  3.     public static void updateInsertLead(List<Lead> leadObjList){
  4.         Set<String> setStr = NEW Set<String>();
  5.          List<Lead> leadObj = NEW List<Lead>();
  6.         List<Lead> leadList=[SELECT Id, Name, Email, Phone FROM Lead WHERE Email != NULL];
  7.         FOR(Lead d1:leadList){
  8.             setStr.add(d1.Email);
  9.         }
  10.  
  11.           FOR(lead e1:leadObjList){
  12.                 IF(setStr.contains(e1.Email)){
  13.                     e1.Email.addError('Do not allow duplicate Email');
  14.                 }
  15.             }
  16.  
  17.     }
  18.  
  19.  
  20. }

Apex Trigger to Check Duplicate Email on Lead Object -- w3web.net
 

 

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

How can you avoid maximum trigger depth exceeded?

To avoid these kind of situation we can use public class static variable. We can solve this issue, you can set a condition on trigger so it will not be called recursively.

Why is my trigger running twice?

Triggers can fire twice, once before workflows and once after workflows.

How many times trigger will fire in Salesforce?

Triggers execute on batches of 200 records at a time. So if 400 records cause a trigger to fire, the trigger fires twice, once for each 200 records.

Related Topics | You May Also Like

 
  

Our Free Courses β†’

πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration




Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment