On a VF page, I am making use of Custom Metadata Types and displaying some text to the end users which is going to the input for: setplainTextBody(subBody)
to enable capability for editing it before sending out the email.
Using Messaging.SingleEmailMessage();
to send out the email to the contacts that are selected from below Wrapper class. I want to make this subBody
to be one of the merge fields on an email template along with some standard fields: ContactName, Phone and other custom fields. Currently with this: mail.setplainTextBody(subBody);
I can only send whatever data in that text area.
How can I implement this:
Dear {!Contact.FirstName},
{!Case.Email_Body__c} "This is subBody"
Case details:
Record Name: {!Kindle__c.Name}
Type of Record: {!Case.Kindle_Type__c}
Record title: {!Case.Subject} Kindle officer's Contact number: {!Case.KindleConNumb__c}
Extension(If any): {!Case.Extension__c}
Thank you, {!Kindle__c.OwnerFullName}
EDIT: This is my new implementation which ended up with an error:
I am getting an error: SendEmail failed. First exception on row 0; first error: EMAIL_TEMPLATE_MERGEFIELD_ERROR, Body: Field cw.con.Name does not exist. Check spelling.: [cw.con.Name]. This is my code:
if(!atLeastOneSelected){
for(cContact cw: contactList){
if(cw.selected == TRUE){
atLeastOneSelected = true;
System.debug('Select Con:' +cw.con.Name);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTreatBodiesAsTemplate(true);
mail.setToAddresses(new String[] {cw.con.Email});
mail.setplainTextBody('Dear {!cw.con.Name}' +subBody );
mail.setSenderDisplayName('Kindler');
mail.setSubject('Kindle's next Best');
listEmail.add(mail);
contactList = null;
}
}
}
Using the System.debug
I could see the selected contact's name, but ended up with this error.