EmailTemplateSelector Interface

The Support.EmailTemplateSelector interface enables providing default email templates in Case Feed. With default email templates, specified email templates are preloaded for cases based on criteria such as case origin or subject.

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

Support

To specify default templates, you must create a class that implements Support.EmailTemplateSelector.

When you implement this interface, provide an empty parameterless constructor.

EmailTemplateSelector Methods

The following are methods for EmailTemplateSelector.

getDefaultTemplateId(caseId)

Returns the ID of the email template to preload for the case currently being viewed in the case feed using the specified case ID.

Signature

public ID getDefaultTemplateId(ID caseId)

Parameters

  • caseId:

    Type: ID

Return Value

Type: ID

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.

1public with sharing class MyCaseTemplateChooser implements Support.EmailTemplateSelector {
2    // Empty constructor 
3    public MyCaseTemplateChooser() {    }
4
5    // The main interface method 
6    =public ID getDefaultEmailTemplateId(ID caseId) {
7        // Select the case we're interested in, choosing any fields that are relevant to our decision
8        Case c = [SELECT Subject, Description FROM Case WHERE Id=:caseId WITH USER_MODE];
9
10        EmailTemplate et;
11
12        if (c.subject.contains('LX-1150')) {
13            et = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1150_template' WITH USER_MODE];
14        } else if(c.subject.contains('LX-1220')) {
15            et = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1220_template' WITH USER_MODE];
16        } 
17        
18        // Return the ID of the template selected
19        return et.id;
20    }
21}

The following example tests the above code:

1@IsTest
2private with sharing class MyCaseTemplateChooserTest {
3
4    @IsTest
5    static void testChooseTemplate() {
6    
7        MyCaseTemplateChooser chooser = new MyCaseTemplateChooser();
8
9        // Create a simulated case to test with
10        Case c = new Case();
11        c.Subject = 'I\'m having trouble with my LX-1150';
12        Database.insert(c, AccessLevel.USER_MODE);
13            
14        // Make sure the proper template is chosen for this subject
15        Id actualTemplateId = chooser.getDefaultEmailTemplateId(c.Id);
16        EmailTemplate expectedTemplate = 
17          [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1150_template' WITH USER_MODE];
18        Id expectedTemplateId = expectedTemplate.Id;
19        Assert.areEqual(expectedTemplateId, actualTemplateId);
20  
21        // Change the case properties to match a different template
22        c.Subject = 'My LX1220 is overheating';
23        Database.update(c, AccessLevel.USER_MODE);
24  
25        // Make sure the correct template is chosen in this case
26       actualTemplateId = chooser.getDefaultEmailTemplateId(c.Id);
27        expectedTemplate = 
28          [SELECT id FROM EmailTemplate WHERE DeveloperName = 'LX1220_template' WITH USER_MODE];
29        expectedTemplateId = expectedTemplate.Id;
30        Assert.areEqual(expectedTemplateId, actualTemplateId);
31    
32    }
33}