0

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.

1 Answer 1

2

You should look at the setTreatBodiesAsTemplate method.

Optional. If set to true, the subject, plain text, and HTML text bodies of the email are treated as template data.

Example usage:

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setPlainTextBody('Sending a test to {!Contact.FirstName}');
email.setTreatBodiesAsTemplate(true);

// set other properties
// send email
4
  • As I already have my email template ready, can I make use of that template(by providing its ID) to send out the email to the selected contacts from the VF page?
    – Arnold Jr.
    Aug 29, 2017 at 17:05
  • @ArnoldJr. You may have to query the template body, manipulate it, then pass it to your email message instance.
    – Adrian Larson
    Aug 29, 2017 at 17:40
  • I was able to query the EmailTemplate and set: mail.setTemplateId(mailTemp.Id);, but how can I manipulate it's body now?
    – Arnold Jr.
    Aug 29, 2017 at 17:57
  • Any suggestions?
    – Arnold Jr.
    Aug 31, 2017 at 4:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .