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
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC) 

How to send a email in trigger

Hi All i have a requirement like this an lead should be created once the lead created a contact should be created and lead should be deleted
1st part is working fine and for second part: 
if we have exisitng contact with same email and an email should be sent for the user and it should be updates with exisitng contact
Can any one help with this :
Please see my code below :
trigger CreateContactTest on Lead (After Insert) 
{
//Map<string,Id> mapaddress = New Map<String,Id>();
List<Lead> toDelete =New List<Lead>();
Set<string> Email= new Set<string>();
List<Contact>Contacts=[Select Id,Email from Contact Where Email=:Email];
 List <Contact>Con= new List<Contact>();
 
 For( Lead LD:Trigger.new)
 { 

 Contact cc = New Contact();
 cc.FirstName= LD.FirstName;
 cc.LastName=LD.LastName;
// cc.Name=LD.Name;
 cc.Salutation=LD.Salutation;
 cc.Email=LD.Email;
 cc.Title=LD.Title;
 cc.Phone=LD.Phone;
 
 Con.add(cc);
 Todelete.Add(LD);
 
  
 }
 insert con;
 if(todelete.size()>0)
 Delete [Select ID from lead where Id In:ToDelete];
 }
ManojjenaManojjena
Hi Gattam,

Try with below code it wil help !!
trigger CreateContactTest on Lead (after Insert){
    List<Messaging.SingleEmailMessage> emailList=new List<Messaging.SingleEmailMessage>();
    List<Contact> conListForManipulation=new List<Contact>();
    Map<String,Contact> conEmailMap=new Map<String,Contact>();
    Set<Id> ledSetToDelete=new Set<Id>();
    Set<String> emailSet=new Set<String>();
    for(Lead led :Trigger.new ){
        if(led.Email != null){
            emailSet.add(led.Email);
        }
    }
    List<Contact> conList=[SELECT Id,Email FROM Contact WHERE Email IN : emailSet];
    if(conList.size() > 0){
        for(Contact con : conList ){
           conEmailMap.put(con.Email,con);
        }
    }
    for(Lead led :Trigger.new ){
        if(conEmailMap.containsKey(led.Email)){
            Contact conn = New Contact();
            conn.Id=conEmailMap.get(led.Email).Id;
            conn.FirstName= led.FirstName;
            conn.LastName=led.LastName;
            conn.Salutation=led.Salutation;
            conn.Email=led.Email;
            conn.Title=led.Title;
            conn.Phone=led.Phone;
            conListForManipulation.add(conn);
             
        }else{
            Contact cc = New Contact();
            cc.FirstName= led.FirstName;
            cc.LastName=led.LastName;
            cc.Salutation=led.Salutation;
            cc.Email=led.Email;
            cc.Title=led.Title;
            cc.Phone=led.Phone;
            conListForManipulation.add(cc);
        }
         ledSetToDelete.add(led.Id);
    }
    if(ledSetToDelete.size() >0){
       try{
          Delete [Select ID from lead where Id IN:ledSetToDelete];
       }catch(DmlException de){
          System.debug(de.getMessage());
       }
    }
    if(!conEmailMap.isEmpty()){
        for(Contact con :conEmailMap.values() ){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            sendTo.add(con.Email);
            mail.setToAddresses(sendTo);
            mail.setSubject('Contact in your name got updated');
            mail.setHtmlBody('Here add your body');
           emailList.add(mail);
        }
    }
    Messaging.sendEmail(emailList);
    try{
    upsert(conListForManipulation);
   }catch(DmlException de){
    System.debug(de);
} 
}
Let me know if it helps !!
Thanks 
Manoj