function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nazli FatimaNazli Fatima 

When lead is created or updated then check if the email of lead is already there in existing contacts. If email already exist then throw an error.

can any one please help me in this code. I am having a error.

Trigger :

trigger Trg_CheckLeadEmail on Lead (before insert,before update) {  
if(trigger.isBefore  && trigger.isInsert || Trigger.isUpdate))
{
    Trg_ClassCheckLeadEmail.CheckEmail(Trigger.new,Trigger.oldMap);
  }
}


Class:
public class Trg_ClassCheckLeadEmail {

 public static void CheckEmail ( List<Lead> newLeadList, map<String, Contact> OldMapContacts){
    
    // List<Lead> newLeadList= new list<Lead>();
    map<String,Contact>  ContMapSet = new map<String, Contact>();
    list<Contact> conlst = [SELECT Id, Email FROM Contact];
    
    for (Contact cont:conlst) {
        ContMapSet.put(cont.Email, cont);
    } 
    for(lead led : newLeadList) 
    {
        if((led.Email != null) && (trigger.isInsert || (led.Email != OldMapContacts.get(led.id).Email))) 
        {
            if(ContMapSet.containsKey(led.Email))
            {
                led.Email.addError('Email Already Exists !!!');
            }
        }               
       }
    }
}


Error: Compile Error: Method does not exist or incorrect signature: void CheckEmail(List<Lead>, Map<Id,Lead>) from the type Trg_ClassCheckLeadEmail at line 7 column 29
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below trigger and handler. 

The error you faced is because always Trigger.old will give old map f<id,Sobject> on which the trigger is written which means Map<id, Lead> and modified the apex class as well as per your requirement.

Trigger:
trigger Trg_CheckLeadEmail on Lead (before insert,before update) {  
if(trigger.isBefore  &&( trigger.isInsert || Trigger.isUpdate))
{
    Trg_ClassCheckLeadEmail.CheckEmail(Trigger.new,Trigger.oldMap);
  }
}

Handler:
public class Trg_ClassCheckLeadEmail {

 public static void CheckEmail ( List<Lead> newLeadList, map<id, Lead> OldMapLead){
    
    // List<Lead> newLeadList= new list<Lead>();
  Map<String,Lead>maplead= New Map<String,Lead>();
    
    for (Lead ld:newLeadList) {
        maplead.put(ld.Email, ld);
    } 
    List<Contact> Conlist= [select id,Email from Contact where Email in :maplead.keyset()];
     
     For(Contact con:Conlist){
         
         for(Lead lea:newLeadList){
             
             if(maplead.containskey(con.email)){
                 lea.adderror('Email Already Exists !!!');
             }
         }
         
     }
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
mukesh guptamukesh gupta
Hi Nazil,

Please use below code:-

Trg_CheckLeadEmail
trigger Trg_CheckLeadEmail on Lead (before insert,before update) {  
if(trigger.isBefore  && trigger.isInsert || Trigger.isUpdate))
{
    Trg_ClassCheckLeadEmail.CheckEmail(Trigger.new);
  }
}

Trg_ClassCheckLeadEmail
public class Trg_ClassCheckLeadEmail {

 public static void CheckEmail ( List<Lead> newLeadList){
    Set<Id> conIds = new Set<Id>();
     for(Lead l : newLeadList)
     {
       conIds.add(l.WhoId);         
     }
    Map<Id,Contact> mapConlst = New Map<Id,Contact>([SELECT Id, Email FROM Contact where  Id IN: conIds]);
     
     for(Lead l : newLeadList){
         if(mapConlst.get(l.whoId).Email == l.email){
             l.Email.addError('Email Already Exists !!!');
         }
     }
  }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh