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
RK29RK29 

Trigger on Leads that checks to see if another Lead or Contact has the exact same name.

Hi All,
I have created a trigger that checks to see if another Lead or Contact has the exact same name. How can I avoid the for loop inside for loop to compare old and new lead values and avoid running SOQL inside loop to get matching contacts? Code below:
 
trigger DuplicateLeadOrContact on Lead (before insert) {
    list<Lead> leadList = new list<Lead>();
    list<Contact> conList = new list<Contact>();
    leadList = [SELECT Id, firstname, lastname from LEAD];
    for(Lead myLead : Trigger.New){
        if(myLead.firstName != null && myLead.lastName != null){
            for(Lead oldLead : leadList){
                if(myLead.firstName == oldLead.firstname && myLead.lastName == 
           oldLead.lastName){
                    myLead.addError('Lead already Exists with same Lead first 
               name and last name');
                }

            }

        }
        conList = [SELECT Id, firstname, lastname from Contact where firstname 
        =:myLead.firstName AND lastName =:myLead.lastName];
        if(conList.size() > 0){
            myLead.addError('Lead already Exists with same Contact first name 
        and last name');
        }
       }


}

 
Best Answer chosen by RK29
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi ,

Below code may help you.
 
/*
    avoid duplicates in Lead and contact
    
    bulkified, and with all best practices
*/
trigger DuplicateLeadOrContact on Lead (before insert) {
    Set<String> firstNameSet = new Set<String>();
    Set<String> lastNameSet = new Set<String>();
    
    Map<String,Lead> furstNameLastNameLeadMap = new Map<String,Lead>();          // i took map , bcz if you want to show some record information with error messagef, map will help
    Map<String,Contact> furstNameLastNameContactMap = new Map<String,Contact>();// i took map , bcz if you want to show some record information with error messagef, map will help
    for(Lead ld : Trigger.New){
        firstNameSet.add(ld.firstname);
        lastNameSet.add(ld.lastname);
    }
    for(Lead ld : [SELECT Id, firstname, lastname from LEAD WHERE firstName IN : firstNameSet AND lastName IN: lastNameSet]){
        furstNameLastNameContactMap.put(ld.firstname + ld.lastname,ld);
    }
    for(Contact con : [SELECT Id, firstname, lastname from Contact where firstname =:myLead.firstName AND lastName =:myLead.lastName]){
        furstNameLastNameContactMap.put(con.firstname+con.lastname,con);
    }
    
    for(Lead ld : Trigger.New){
        if(furstNameLastNameContactMap.containsKey(ld.FirstName + ld.LastName)){
            ld.addError('Lead already Exists with same Lead first name and last name'); 
        }
        if(furstNameLastNameContactMap.containsKey(ld.FirstName + ld.LastName)){
            ld.addError('Lead already Exists with same Contact first name and last name');
        }
    }
    
}//end of  trigger


Regards
Naveen

 

All Answers

Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi ,

Below code may help you.
 
/*
    avoid duplicates in Lead and contact
    
    bulkified, and with all best practices
*/
trigger DuplicateLeadOrContact on Lead (before insert) {
    Set<String> firstNameSet = new Set<String>();
    Set<String> lastNameSet = new Set<String>();
    
    Map<String,Lead> furstNameLastNameLeadMap = new Map<String,Lead>();          // i took map , bcz if you want to show some record information with error messagef, map will help
    Map<String,Contact> furstNameLastNameContactMap = new Map<String,Contact>();// i took map , bcz if you want to show some record information with error messagef, map will help
    for(Lead ld : Trigger.New){
        firstNameSet.add(ld.firstname);
        lastNameSet.add(ld.lastname);
    }
    for(Lead ld : [SELECT Id, firstname, lastname from LEAD WHERE firstName IN : firstNameSet AND lastName IN: lastNameSet]){
        furstNameLastNameContactMap.put(ld.firstname + ld.lastname,ld);
    }
    for(Contact con : [SELECT Id, firstname, lastname from Contact where firstname =:myLead.firstName AND lastName =:myLead.lastName]){
        furstNameLastNameContactMap.put(con.firstname+con.lastname,con);
    }
    
    for(Lead ld : Trigger.New){
        if(furstNameLastNameContactMap.containsKey(ld.FirstName + ld.LastName)){
            ld.addError('Lead already Exists with same Lead first name and last name'); 
        }
        if(furstNameLastNameContactMap.containsKey(ld.FirstName + ld.LastName)){
            ld.addError('Lead already Exists with same Contact first name and last name');
        }
    }
    
}//end of  trigger


Regards
Naveen

 
This was selected as the best answer
RK29RK29
Thanks Naveen. This is the exact approach, I was looking for.