You need to sign in to do that
Don't have an account?
Roopa S 1
Write a trigger on Contact , when a contact insert an email should be sent to contact email id with specified template. So first you need to create text template.Choose any template design
how to write code for this
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try below handler and trigger.
class:
public class sendEmailHandlerclass {
//static method
public static List<Contact> sendEmail(List<Contact> contacts) {
//query on template object
EmailTemplate et=[Select id,Body from EmailTemplate where name=:'Sales: New Customer Email'];
//list of emails
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
//loop
for(Contact con : contacts){
//check for Account
if(con.AccountId == null || con.Email != null){
//initiallize messaging method
Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
singleMail.setHtmlBody(et.Body);
//set object Id
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to false to stop inserting activity history
singleMail.setSaveAsActivity(false);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return contacts;
}
}
trigger:
trigger sendEmail on Contact (after insert) {
if(Trigger.isAfter){
if(Trigger.isInsert ){
//helper class for single email but bulk messages
sendEmailHandlerclass.sendEmail(trigger.new);
}
}
}
Thanks,
Anshi.
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
We have to create a email template called "Sample one" as per the below code.
Please find the below Code.
Trigger
Handler:
If this solution helps, Please mark it as best answer.
Thanks,
while saving contact getting error saying "sendMailToContactTrigger: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.sendMailToContactTrigger: line 3, column 1"
while saving contact getting this error "ContactEmailTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Class.sendMailToContactTriggerHandler.sendLeadOverlapCompleteNotification: line 13, column 1 Trigger.ContactEmailTrigger: line 4, column 1"
Make Sure You Have Template In Your Org With the Name You Are Querying
Thank You!