Hey guys, today in this post we are going to learn about Write Apex Trigger Scenarios, Before creating a Lead, we will check Email, if email already exist, then we will display error message, do not allow create duplicate Lead in Salesforce.
Final Output β
Other related post that would you like to learn in Salesforce
Create Apex Trigger β
Step 1:- Create Apex Controller : LeadTrigger.apxt
SFDX:Create Apex Trigger>> New >> LeadTrigger.apxt
LeadTrigger.apxt [Apex Trigger]
TRIGGER LeadTrigger ON Lead (BEFORE INSERT, BEFORE UPDATE) {
IF(TRIGGER.isBefore){
IF(TRIGGER.isInsert){
LeadTriggerHandler.duplicateEmailLead(TRIGGER.new);
}ELSE IF(TRIGGER.isUpdate){
LeadTriggerHandler.duplicateEmailLead(TRIGGER.new);
}
}ELSE IF(TRIGGER.isAfter){
}
}
Create Trigger Handler Controller β
Step 2:- Create Apex Controller : LeadTriggerHandler.apxc
SFDX:Create Apex Trigger>> New >> LeadTriggerHandler.apxc
LeadTriggerHandler.apxc [Trigger Handler]
public class LeadTriggerHandler {
public static void duplicateEmailLead(List<Lead> leadList){
Map<String, Lead> leadMap = NEW Map<String, Lead>();
List<Lead> leadObjList = [SELECT Id, Name, Email FROM Lead WHERE Email !=NULL];
FOR(Lead L1:leadObjList){
leadMap.put(L1.Email, L1);
}
FOR(Lead L2:leadList){
IF(leadMap.containskey(L2.Email)){
L2.addError('Do not allow duplicate Lead, this email is already exist');
}
}
}
}
Further post that would you like to learn in Salesforce
How to create an email to lead?
Email to Lead or Email2Lead or LeadFromEmail - creates a Lead object from an inbound email. Simply setup an EmailService for this class, setup a forward rule on your inbound sales email address to this EmailService inbound email address, that's it you are done.
How to create a lead process?
From Setup, enter Lead Process in the Quick Find box, select Lead Processes, and then click New. Select Master in the Existing Lead Process field. Enter External Contact Tracing in the Lead Process Name field. Enter a description for the process such as Process to add status to leads.
How many ways can you create a lead in Salesforce?
To create a lead in Salesforce, you can manually enter data, use a web-to-lead form, or automate with the Salesforce API. Manually, navigate to the Leads tab, click 'New', fill in the lead's details, and save. For web-to-lead, enable the feature in Setup, create a form, and embed the generated HTML on your site.
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 |