In Salesforce’s Winter '13 release, the Case Feed UI is gaining a very powerful customization option: Smart Templates.

In the old world of customer service, agents must manually select the correct email template - potentially scouring many libraries to do so.  Now, when an agent opens a case, the correct email template is automatically loaded based on the recipient’s profile. For example: new cases vs. escalated cases or for a first response to a customer vs. for an ongoing communication thread.  This can greatly improve your agents’ efficiency since so often they need them to use different templates for different cases.

The goal of this blog is to describe a sample implementation, where a matching logic is coded in Apex and the configuration stored in a custom settings object.

The logic is based on using two standard case attributes, Case Status and Case Reason, and it’s very easy to modify in order to use other case attributes or contextual information like case contact fields.

Let's first have a look at the main requirement for the Apex class that supports Smart Templates: it needs to implement the new Support.EmailTemplateSelector interface.

global class SmartTemplateLoader implements Support.EmailTemplateSelector {
// Empty constructor
global SmartTemplateLoader() {
}
// The main interface method
global ID getDefaultEmailTemplateId(ID caseId) {
return null;
}
}

Before diving into the details of the code logic, we should create the custom settings object that will store the configuration. We want our logic to be based on two case attributes, and each combination will need a corresponding email template. So let's create a new custom setting, Smart Template Conf, which will have three fields:

  • Case Status--type: text, length: 50
  • Case Reason--type: text, length: 50
  • Template Name--type: text, length: 100

Follow these steps:

      1.  Click {Your Name}>Setup>Develop>Custom Settings.
      2.  Click New.
      3.  Enter this information:
              a.  Label: Smart Template Loader Conf
              b.  Name: Smart_Template_Loader_Conf
              c.   Setting type: List
              d.  Visibility: Public
      4.  Click Save.
      5.  Click New in the custom fields section.
      6.  Select Text and then click Next.
      7.  Enter this information:
              a.  Label: Case Status
              b.  Length: 50
              c.  Name: Case_Status
      8.  Click Next and then click Save.
      9.  Repeat steps 5 to 8 for the other two fields:
              a.  Case Reason (length 50)
              b.  Template Name (length 100)

Smart template loader - fields

We will see later in this blog how to use this custom settings object to configure the Smart Template loader.

Coming back to the code logic, we want to implement the template matcher, using some precedence in the matching algorithm:

  • Return the configuration that matches both Case Status and Case Reason
  • If no match, return the configuration that matches the Case Status
  • If no match, return the configuration that matches the Case Reason
  • If no match, return a default template
  • If no match, return null (no template loaded)


Here’s the full source code for our template loader:

global class SmartTemplateLoader implements Support.EmailTemplateSelector {
// Empty constructor
global SmartTemplateLoader() { }
// 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 Status, Reason FROM Case WHERE Id=:caseId];

Smart_Template_Loader_Conf__c conf = null;
List<Smart_Template_Loader_Conf__c> confs = null;

// get template from case status + case reason
confs = [SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE Case_Status__c = :c.status and Case_Reason__c = :c.reason];
if (!confs.isEmpty()) {
conf = confs[0];
System.debug('got template: ' + conf.Template_Name__c);
}

// get template from case status
if (conf==null) {
confs = [SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE Case_Status__c = :c.status];
if (!confs.isEmpty()) {
conf = confs[0];
System.debug('got template: ' + conf.Template_Name__c);
}
}
// get template from case reason
confs = [SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE Case_Reason__c = :c.reason];
if (!confs.isEmpty()) {
conf = confs[0];
System.debug('got template: ' + conf.Template_Name__c);
}

// get default template
if (conf==null) {
confs = [SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE Case_Status__c = '' AND Case_Reason__c = ''];
if (!confs.isEmpty()) {
conf = confs[0];
System.debug('got template: ' + conf.Template_Name__c);
}
}

if (conf==null) {
System.debug('no template!');
return null;
}

// get the template ID
List<EmailTemplate> ets = [SELECT id FROM EmailTemplate WHERE DeveloperName = :conf.Template_Name__c];
//Return the ID of the template selected
if (!ets.isEmpty()) {
EmailTemplate et = ets[0];
System.debug('template: ' + conf.Template_Name__c + ' -- id: ' + et.id);
return et.id;
}
System.debug('No template with the name: ' + conf.Template_Name__c);

return null;
}
}

(The complete source code, including a test class, is available on github.)

The next step is to configure our Smart Template loader. First, be sure to have some email templates available for your use case. For example, we’ll use the following templates (these are the developer names for the templates):

  • NewCase_ComplexFunctionality
  • NewCase_ExistingProblem
  • EscalatedCase
  • Default

In order to create/manage the configuration:

      1.  Click {Your Name}>Setup>Develop>Custom Settings.
      2.  Click Manage.
      3.  Click New.
      4.  Fill in the fields. Note: the Name field needs to be descriptive enough so you can recognize it easily, but it’s not referenced in the code.
      5.  Click Save.

The sample configuration looks like this:

Smart template loader - config

The last step of the configuration is to declare our Smart Template Loader in the Case settings:

     1.  Click {Your Name}>Setup>Develop>Custom Settings.
      2.  Click Cases.
      3.  Click Support Settings.
      4.  Click New.
      5.  Scroll down toward the bottom of the page.
      6.  Check Enable default email templates.
      7.  Use the lookup for the field Apex Class Name and select our SmartTemplateLaoder class.
      8.  Click Save.

How can we test our Smart Template loader? It’s very simple:

  • Open a case in Case Feed.
  • Click Answer Customer, and then select Email. You should see a pre-loaded template.
  • Click View Case Details.
  • Click Edit and change the case status and/or the case reason.
  • Click Save, then open the Email publisher and be sure the template that’s pre-loaded is the one you expected.

Please share your feedback, then fork the code on github and contribute your enhancements!

Service cloud ebook