WAT on Contact when contact insert then send mail to that contact Email with EmailTemplate

trigger sendMailAfterContactInsert on Contact (after Insert) {

    List<Contact> lstContact= new List<Contact>();
    List<Messaging.SingleEmailMessage> lstEmailId=new List<Messaging.SingleEmailMessage>();
    for(Contact con:trigger.new)
    {
      lstContact.add(con);
    }
    EmailTemplate emailTemp = [Select id from EmailTemplate where name= 'MyTestTemp'];
    for(Contact conObj : lstContact)
    {
        Messaging.SingleEmailMessage msgObj = new Messaging.SingleEmailMessage();
         if(conObj.Email!=null)
         {
             msgObj.setTargetObjectId(conObj.Id);
             msgObj.setTemplateId(emailTemp.Id);
             lstEmailId.add(msgObj);
         }  
         else
         {
             conObj.Email.addError('Plz enter Email Id');
         }
       
    }
    if(lstEmailId.size()>0)
    {
        Messaging.SendEmail(lstEmailId);
    }
}

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page