[SalesForce] How to Email Template be created in unit tests
Can I create an Email Template as part of the unit test? If yes, how?
Best Answer
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)
The way to test this would be as follows. Note that SeeAllData is set to false as we are inserting the records in the testmethod.
@isTest(SeeAllData=false)
public class TestDeleteClass {
static testmethod void testDelete() {
sObject abc = new sObject(RequiredField = 'test');
insert abc;
DeleteRecords dr = new DeleteRecord();
Test.startTest();
dr.DeleteAllRecords();
Test.stopTest();
system.assert([select ID from myobject__c].IsEmpty());
}
}
Code coverage simply means that the lines of code were executed by your test method. It doesn't mean that your code is working correctly, you have to write asserts to verify that.
Approval Comment Merge Fields are Available in All Approvals-Related Email Alerts
The {!ApprovalRequest.Comments} merge field can be used in any approvals-related email alert.
Available in: Enterprise, Performance, Unlimited, and Developer Editions
Previously, all approval merge fields named {!ApprovalRequest.field_name} returned
values only when used in an approval assignment email template.
When used in templates for other email alert actions—in workflow rules or approval processes—
the approval merge fields returned null.
Now, for example, if a request is rejected at any approval step,
you can include the approver’s rejection comment in an email alert to the submitter.
Note: The {!ApprovalRequest.Comments} merge field returns only the most recently
entered comment in emails for an approval step that requires unanimous approval from
multiple approvers.
So, it depends on both which merge field you are using and whether you are using the merge field in a template used during approval assignment or in some approval action email alert
Best Answer
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)