You need to sign in to do that
Don't have an account?
Nazli 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
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
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:
Handler:
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Please use below code:-
Trg_CheckLeadEmail
Trg_ClassCheckLeadEmail
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh