Testing email template in SFDC Anonymous Window

How to Test email template in SFDC Anonymous Window ?

Code

In normal case, we will use email Template to test our email in clicking ‘Send Test and verify merge fields’:

alt tag

However, it sometimes, will have following result.

alt tag

To testing email in Production env, we can use following code to test in Apex Anonymous Window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage(); 

//this set user Id
mail.setTargetObjectId('005D0000008fQd4');

//This set Template Id
mail.setTemplateId('00XD0000002J0Ma');

//this set record Id
mail.setWhatId('a9ED00000000kNE');
mail.setCCAddresses( new String[]{'you@email.com'});
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setReplyTo('you@email.com');

mail.setSenderDisplayName('Tony Testing in Production');

mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

For how to use Apex Anonymous Window, you can perdorm with following path: Debug–>Open Execute Anonymous Window ( Ctrl+E).
Or view following post:

Sending email in Salesforce