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);
}
}
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
Post a Comment