EmailTemplateSelector Interface
Support.EmailTemplateSelector works only in Salesforce Classic, not in Lightning Experience. Lightning Experience users can specify default values for emails using the QuickActionDefaultsHandler interface.
Namespace
To specify default templates, you must create a class that implements Support.EmailTemplateSelector.
When you implement this interface, provide an empty parameterless constructor.
EmailTemplateSelector Example Implementation
This is an example implementation of the Support.EmailTemplateSelector interface.
The getDefaultEmailTemplateId method implementation retrieves the subject and description of the case corresponding to the specified case ID. Next, it selects an email template based on the case subject and returns the email template ID.
global class MyCaseTemplateChooser implements Support.EmailTemplateSelector {
// Empty constructor
global MyCaseTemplateChooser() { }
// The main interface method
global ID getDefaultEmailTemplateId(ID caseId) {
// Select the case we're interested in, choosing any fields that are relevant to our decision
Case c = [SELECT Subject, Description FROM Case WHERE Id=:caseId];
EmailTemplate et;
if (c.subject.contains('LX-1150')) {
et = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1150_template'];
} else if(c.subject.contains('LX-1220')) {
et = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1220_template'];
}
// Return the ID of the template selected
return et.id;
}
}
The following example tests the above code:
@isTest
private class MyCaseTemplateChooserTest {
static testMethod void testChooseTemplate() {
MyCaseTemplateChooser chooser = new MyCaseTemplateChooser();
// Create a simulated case to test with
Case c = new Case();
c.Subject = 'I\'m having trouble with my LX-1150';
Database.insert(c);
// Make sure the proper template is chosen for this subject
Id actualTemplateId = chooser.getDefaultEmailTemplateId(c.Id);
EmailTemplate expectedTemplate =
[SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1150_template'];
Id expectedTemplateId = expectedTemplate.Id;
System.assertEquals(actualTemplateId, expectedTemplateId);
// Change the case properties to match a different template
c.Subject = 'My LX1220 is overheating';
Database.update(c);
// Make sure the correct template is chosen in this case
actualTemplateId = chooser.getDefaultEmailTemplateId(c.Id);
expectedTemplate =
[SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1220_template'];
expectedTemplateId = expectedTemplate.Id;
System.assertEquals(actualTemplateId, expectedTemplateId);
}
}