+ Start a Discussion
Bhuvanakruthi MudholeBhuvanakruthi Mudhole 

Create a field 'Search result' on Lead.When lead is updated then check if the email of lead is matching with any contact.If matching set the field as 'Exact match' or else 'No Match'

Jithesh VasudevanJithesh Vasudevan
Hi Bhuvanakruthi,


Try this code,

trigger FindDupes on Lead (before insert, before update)
{
 for(Lead myLead:Trigger.new)
 {
  if(myLead.Email!=null)
  {
   List<Contact> dupes=[Select Id from Contact where Email =:myLead.Email];
   
   if(dupes.size()>0)
   {
    //Assigning the value for Search Result for the if part
    myLead.Search_result__c='Exact match';
       
   }
   else
   {
    //Assigning the value for Search Result for the else part
    myLead.Search_result__c='No match';
   }
  }
 
 }


}
Akshay_DhimanAkshay_Dhiman
Hi Bhuvanakurthi,
This code will fulfill your requirement --
 
trigger LeadTrigger on Lead (before update) {
    Map<String,Contact> mapConList = new  Map<String,Contact>();
    List<contact> conlist = new List<contact>();
    List<String> strListEmail = new List<String>();
    for(Lead ll : Trigger.New)
    {
        strListEmail.add(ll.Email);
        
    }
    conlist = [Select Id,Name,Email from Contact where Email IN :strListEmail ];
    if(conlist.size()>0)
    {
        for(Contact con : conlist)
        {
            if(!mapConList.containsKey(con.Email))
            {
                mapConList.put(con.Email, con);
            }
        }
    }
   
        for(Lead leadList : Trigger.New)
        {
            if(mapConList.containsKey(leadList.Email))   
            {
                leadList.Search_result__c = 'Exact Match';
            }
            else
            {
                leadList.Search_result__c = 'No Match';
                
            }
        }
}



If found this helpful mark it as best so that it helps others in solving the same.

Thanks 
Akshay
VamsiVamsi
Hi,

Please find the bulkified version of code ..
 
Trigger SearchCon on Lead (After Insert, After update)
{
        Map<ID,string> LeadMap =  new Map<ID,string>();
        for(Lead l: Trigger.new)
		{
		    if(l.Email!=null)
			{
			    if(Trigger.isupdate)
				{
				    if(Trigger.oldmap.get(l.id).Email!= l.Email)
					{
					    LeadMap.put(l.id,l.email);
					}
				}else
				{
				    LeadMap.put(l.id,l.email);
				}
			}
		}
		
		Map<string,integer> ContactCount =  new Map<string,count>();
		
		for(AggregateResult ag : [select count(id)Total,Email from contact where Email IN : LeadMap.values() GroupBy Email])
		{
		       ContactCount.put(ag.get('email'),ag.get('total'));
		}
		
		List<Lead> FinalLeadList = new List<Lead>();
		for(Lead le : [select Search_result__c, Email from Lead where ID IN : LeadMap.Keyset()])
		{
		       if(ContactCount.containsKey(le.email) && ContactCount.get(le.Email) >0)
			   {
			      le.Search_result__c = 'Exact Match';
				  FinalLeadList.add(le);
			   }else
			   {
			     le.Search_result__c = 'No Match';
				 FinalLeadList.add(le);
			   }
		}
		
      List<Database.saveResult> LeadupdateResult = Database.update(FinalLeadList,false);

}