3

I have been unable to find any useful documentation on this method outside of the horrible definition that SalesForce gives it that gives you no indication of what it actually does for you.

What use does this method have? When is a time you would want to use it based on its function?

3

It works exactly as the documentation lays out.

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

It basically allows you to create your own template from Apex. Here's a demonstration of how it would work:

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTreatBodiesAsTemplate(true);
// now merge fields will be treated as templates

email.setSubject('Hi {!User.CommunityNickname}');
email.setPlainTextBody('Sending a test to {!User.Name}');
email.setHtmlBody('<p>Sending a test to {!User.Name}</p>');
// the above values will be merged in once the email is sent

email.setTargetObjectId(UserInfo.getUserId());
email.setSaveAsActivity(false);
// you cannot save emails as an activity when the target is a User

Messaging.sendEmail(new List<Messaging.Email> { email });
  • So without that method, you would not be able to do setSubject('Hi {!User.CommunityNickname}') and it would be treated as a literal string for the subject? – Tyler Dahle Jul 26 '17 at 19:07
  • It would be allowed, but nothing would get merged. It's just a literal subject line, yes. – Adrian Larson Jul 26 '17 at 19:09
  • Ah, I see now. So, if you have {!User.Name} in your HTML email template, that will merge regardless of if setTreatBodiesAsTemplates is true or not, however, setting the html body in apex with merge fields would, again, just be strings and disregard the merge fields unless that method was set to true? – Tyler Dahle Jul 26 '17 at 19:12
  • Exactly that, yes. – Adrian Larson Jul 26 '17 at 19:12
  • Alright, that method may indeed be very helpful to me haha. Thank you for the info! SalesForce's short definition made it difficult to really see why you might want to use it, but this has cleared it up for me :) – Tyler Dahle Jul 26 '17 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.