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
SFDC16SFDC16 

trigger not working for existing lead email

Hello Developer,
 
I am trying to create a trigger when a new lead is created it will check lead email in the account exist or not.

If  "YES" then update the account name with the lead name.

 If  "No" the create a new account with lead name.

  Any help would be greatly appreciated.

trigger leadMap on Lead (before insert) 
{
       List<String> lea=new List<String>();
      
       for(lead le:trigger.new)
       {
           lea.add(le.Email);
           
       }   
       System.debug('Email------------------------>'+lea);
    
       list<account> accList=[Select id,name,Email__C from Account where Email__C =:lea];
       System.debug('accList------------->'+accList);
  if(accList.size()>0)
  {
    for(Lead l:trigger.new)
      {
              System.debug('before first for loop');
            
               for(Account ac:accList)
                {
                    System.debug('ac===========>'+ac.name);
                    if(ac.email__C==l.email)
                    {
                        System.debug('ac.email__C===============>'+ac.email__C);
                        System.debug('l.email===================>'+l.email);
                        ac.name=l.LastName;
                        ac.Description='trigger fire';
                        System.debug('Inside if statement');

                    }
                    
                    
                }
            }
      }
          else
          { 
              for(lead ls:trigger.new)
              {
                  Account a=new Account();
                  a.Name=ls.name;
                  accList.add(a);
                  
              }
              
          }
}

Regards,
SFDC16
Ajay K DubediAjay K Dubedi
Hi SFDC16,
Try the below code and used always best practice .
//Trigger
trigger leadMap on Lead (before insert) {
    if(Trigger.isInsert && Trigger.isBefore){
        leadMapHelper.leadMapHelper_method(Trigger.new);
    }        
}

//Helper class
public class leadMapHelper{
    public static leadMapHelper_method(List<Lead> leadList){
        List<String> leadEmail = new List<String>();
        Map<String, Account> stringVsAccount = new Map<String, Account>();
        List<Account> accountListNew = new List<Account>();
        List<Account> accountList = new List<Account>();
        for(lead lObj:trigger.new){
            if(lObj.Email != null){
                leadEmail.add(lObj.Email);
            }
        }
        if(leadEmail.size()>0){
            accountList = [SELECT Id, Name ,Email__C from Account WHERE Email__C =:leadEmail LIMIT 10000];
        }
        if(accountList.size()>0){
            for(Account accObj : accountList){
                stringVsAccount.put(accObj.Email__C, accObj);
            }
                
        }
        if(stringVsAccount.size()>0){
            for(Lead leadobj : leadList){
                if(stringVsAccount.containsKey(leadobj.Email__C)){
                    Account accObj = new Account();
                    accObj = stringVsAccount.get(leadobj.Email__C);
                    accObj.name=leadobj.LastName;
                    accObj.Description='trigger fire';
                    accountListNew.add(accObj);
                }
                else{
                    Account accObj = new Account();
                    accObj.Name=leadobj.name;;
                    accountListNew.add(accObj);
                }
            }
        }
        if(accountListNew.size() > 0){
            upsert accountListNew;
        }
    }

}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi