17

Can I create an Email Template as part of the unit test? If yes, how?

4 Answers 4

5

Unfortunately at this time there isn't any support for creating Email Templates anywhere in APEX, which is the case for most metadata objects. For the most part these can only be manipulated through the browser or the metadata API.

Update this is now possible (see the other answers for more details)

1
  • 1
    This is not actually true ...
    – user735
    Commented Jan 23, 2015 at 21:58
44

I assure you that it IS possible to create an EmailTemplate via Apex for unit testing.

EmailTemplate validEmailTemplate = new EmailTemplate();
validEmailTemplate.isActive = true;
validEmailTemplate.Name = 'name';
validEmailTemplate.DeveloperName = 'unique_name_addSomethingSpecialHere';
validEmailTemplate.TemplateType = 'text';
validEmailTemplate.FolderId = UserInfo.getUserId();

insert validEmailTemplate;

The part that may trip people up is folders: one cannot create an actual folder via Apex as of 23JAN15... but we can leverage the fact that personal folder ID is the user ID, and we definitely have access to the running user in a unit test.

Depending on your test method's DML operations you may have to put the entire test method into a user context via System.runAs(pickYourUser). Code above works as-in even when running under a defined user context.

1
  • trick with user ID as folder Id was exaclty thing I was looking for, works perfectly!
    – patryk
    Commented May 29, 2019 at 7:27
7

The supported calls for an email template are as follows: create(), delete(), describeSObjects(), getDeleted(), getUpdated(), query(), retrieve(), update(), upsert().

The fields are: ApiVersion, Body (Type = textarea), BrandTemplateId, Description, DeveloperName, Encoding, FolderId, HtmlValue (Type = textarea), IsActive, LastUsedDate, Markup, Name, NamespacePrefix, OwnerId, Subject, TemplateStyle, TemplateType, and TimesUsed.

It would appear to me that all you need to do is something like the following:

String NameVar; // holds name of email template
textarea BodyVar; // holds text body of email template 
Id BrandTmpIdVar; 
//Required - supplies letterhead information for the email template
String DevNmVar;
//This is the Label or Email Template's Unique Name.
Id FldrIdVar;
//ID of the folder that contains the template. Prob optional if you don't need to know this
String Sbjct; // holds subject of the Email Template

// Insert your code here to assign values to the above variables

// Now, create the email template

EmailTemplate e = New EmailTemplate(Name = NameVar, Body = Bodyvar, BrandTemplateId=BrandTmpVar, DeveloperName=DevNmVar, FolderId=FldrIdVar, IsActive=True, Subject=Sbjct, TemplateType=Text) ;

Insert e;

Note: I've made the assumption that you're creating a Text email template. Adjust the code accordingly if you want an HTML or VisualForce Template and remove fields as you deem appropriate for your test needs. Refer to the Object Reference for Salesforce and Force.com for more details on each field.

YMMV on the above code. I just wrote the above on the fly using the object reference and have not tested it for verification.

7

@Das Uber answer is great and works fine. I just want to add something:

If, for some reason, you need to test the email template subject (eg. You're testing a class that will manipulate some subject parameter), you must enter the field value as well:

EmailTemplate validEmailTemplate = new EmailTemplate();
validEmailTemplate.isActive = true;
validEmailTemplate.Name = 'name';
validEmailTemplate.DeveloperName = 'unique_name_addSomethingSpecialHere';
validEmailTemplate.TemplateType = 'text';
validEmailTemplate.FolderId = UserInfo.getUserId();
validEmailTemplate.Subject = 'Your Subject Here';


insert validEmailTemplate;

Hope it helps.

You must log in to answer this question.

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