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
shakila Gshakila G 

trigger to check duplicate

I wrote a trigger to prevent the duplicate based on mobile or Email

Requirement 

If Email Not Equal to blank -Check all the Email fields- If records exist with Email -Error -Duplicate Lead

If Email Blank and Mobile Not Equal Blank - Check all the mobile fields- If records exist with the same number -Error -Duplicate Lead
============
My Code is not passing to the second condition ..can anyone help me to out this error
==Always am getting first error======


Trigger leaddup1 on lead(before insert,Before Update)
{

List<Lead> llist=new list<lead>();
List<Lead> elist=new list<lead>();


Set<string> setnumber =new set<String>();
set<string> setemail =new set<string>();

for(Lead l:Trigger.new)
{
setnumber .add(l.MobilePhone);
setnumber .add(l.Whatsapp_Mobile__c);
setnumber .add(l.Mobile_Additional__c);
setnumber .add(l.Phone);
setnumber .add(l.Phone_Additional__c);
setemail.add(l.Email);
setemail.add(l.E_Mail_Additional__c);

}

 IF(setemail!=Null)
    {

        elist=[select ID from Lead where Email =:setemail or        E_Mail_Additional__c=:setemail];
    }
        
 IF(setemail==Null && &&setnumber !=Null)
    {
 llist=[select ID from Lead where MobilePhone =:setnumber or Phone =:setnumber or
 Mobile_Additional__c =:setnumber or Whatsapp_Mobile__c =:setnumber or Phone_Additional__c =:setnumber ];

    }    
       

For(Lead l : Trigger.New)
{
If (Trigger.isinsert)
{
         IF(setemail!=Null && elist.size()>0 )
               {        
                l.adderror('Duplicate Lead -Already Lead Exist with this Email 1');
             }
             
             IF(setemail==Null  && setnumber !=Null &&llist.size()>0)
            {       
                l.adderror('Duplicate Lead -Already Lead Exist with this Mobile 2');
             }
             

}
}

}
Best Answer chosen by shakila G
Amit Chaudhary 8Amit Chaudhary 8
No need to query all Lead record as if record count will more then you will get SOQL error
And dnt query the same record in Update call.

Try to update your code like below
Trigger leaddup1 on lead(before insert, Before Update) {


	Set<String> setEmail = new Set<String>();
	Set<String> setPhone = new Set<String>();
	Set<String> setId = new Set<String>();
	for (Lead l: Trigger.new) {
        if (ld.Phone != null){
			setPhone.add(ld.Phone);
		}
		
        if (ld.Email != null){
			setEmail.add(ld.Email);
		}
		setId.add(ld.id);
	}
		
	List<Lead> lstExistingLead = [Select id,Email from Lead where ( email in :setEmail or Phone in :setPhone ) and id not in :setId];
	
    map < string, Lead > LeadsMapwithPhone = new map < String, Lead > ();
    map < string, Lead > LeadsMapwithEmail = new map < String, Lead > ();

    for (Lead ld: lstExistingLead) {
        if (ld.Phone != null){
            LeadsMapwithPhone.put(ld.Phone, ld);
		}	
        if (ld.Email != null){
            LeadsMapwithEmail.put(ld.Email, ld);
		}	
    }

	for (Lead l: Trigger.new) 
	{
		if ( Trigger.isInsert || (l.Email != trigger.oldMap.get(l.Id).Email ) ) 
		{
			if (l.Email != null && LeadsMapwithEmail.get(l.Email) != null) {
				l.adderror('Lead already exist with ' + l.Email + ' Email. You cannot create a duplicate leads.');
			}
		}

		if ( Trigger.isInsert || (l.Phone != trigger.oldMap.get(l.Id).Phone) )
		{
			if (l.Phone != null && LeadsMapwithPhone.get(l.Phone) != null) {
				l.adderror('Lead already exist with ' + l.Phone + ' Phone. You cannot create a duplicate leads.');
			}
		}
	}
	
}


Let us know if this will help you

All Answers

Waqar Hussain SFWaqar Hussain SF
Hi Shakila,

Please use the below code. And let me know If you have any question.
 
Trigger leaddup1 on lead(before insert, Before Update) {
    list < Lead > ExistingLeads = [Select Id, Name, Email, Phone From Lead Where isConverted = false];
    map < string, Lead > LeadsMapwithPhone = new map < String, Lead > ();
    map < string, Lead > LeadsMapwithEmail = new map < String, Lead > ();

    for (Lead ld: ExistingLeads) {
        if (ld.Phone != null)
            LeadsMapwithPhone.put(ld.Phone, ld);

        if (ld.Email != null)
            LeadsMapwithEmail.put(ld.Email, ld);

    }

    if (trigger.isInsert) {
        for (Lead l: Trigger.new) {
            if (l.Email != null && LeadsMapwithEmail.get(l.Email) != null) {
                l.adderror('Lead already exist with ' + l.Email + ' Email. You cannot create a duplicate leads.');
            }

            if (l.Phone != null && LeadsMapwithPhone.get(l.Phone) != null) {
                l.adderror('Lead already exist with ' + l.Phone + ' Phone. You cannot create a duplicate leads.');
            }
        }
    }


    if (trigger.isUpdate) {
        for (Lead l: Trigger.new) {
            if (l.Email != trigger.oldMap.get(l.Id).Email) {
                if (l.Email != null && LeadsMapwithEmail.get(l.Email) != null) {
                    l.adderror('Lead already exist with ' + l.Email + ' Email. You cannot create a duplicate leads.');
                }
            }

            if (l.Phone != trigger.oldMap.get(l.Id).Phone) {
                if (l.Phone != null && LeadsMapwithPhone.get(l.Phone) != null) {
                    l.adderror('Lead already exist with ' + l.Phone + ' Phone. You cannot create a duplicate leads.');
                }
            }
        }
    }

}

 
Amit Chaudhary 8Amit Chaudhary 8
No need to query all Lead record as if record count will more then you will get SOQL error
And dnt query the same record in Update call.

Try to update your code like below
Trigger leaddup1 on lead(before insert, Before Update) {


	Set<String> setEmail = new Set<String>();
	Set<String> setPhone = new Set<String>();
	Set<String> setId = new Set<String>();
	for (Lead l: Trigger.new) {
        if (ld.Phone != null){
			setPhone.add(ld.Phone);
		}
		
        if (ld.Email != null){
			setEmail.add(ld.Email);
		}
		setId.add(ld.id);
	}
		
	List<Lead> lstExistingLead = [Select id,Email from Lead where ( email in :setEmail or Phone in :setPhone ) and id not in :setId];
	
    map < string, Lead > LeadsMapwithPhone = new map < String, Lead > ();
    map < string, Lead > LeadsMapwithEmail = new map < String, Lead > ();

    for (Lead ld: lstExistingLead) {
        if (ld.Phone != null){
            LeadsMapwithPhone.put(ld.Phone, ld);
		}	
        if (ld.Email != null){
            LeadsMapwithEmail.put(ld.Email, ld);
		}	
    }

	for (Lead l: Trigger.new) 
	{
		if ( Trigger.isInsert || (l.Email != trigger.oldMap.get(l.Id).Email ) ) 
		{
			if (l.Email != null && LeadsMapwithEmail.get(l.Email) != null) {
				l.adderror('Lead already exist with ' + l.Email + ' Email. You cannot create a duplicate leads.');
			}
		}

		if ( Trigger.isInsert || (l.Phone != trigger.oldMap.get(l.Id).Phone) )
		{
			if (l.Phone != null && LeadsMapwithPhone.get(l.Phone) != null) {
				l.adderror('Lead already exist with ' + l.Phone + ' Phone. You cannot create a duplicate leads.');
			}
		}
	}
	
}


Let us know if this will help you
This was selected as the best answer
shakila Gshakila G
Thank you