Case Management brings together ad hoc teams to assess, diagnose and resolve customer issues.  And, SLAs (Service Level Agreements) drive resolution urgency.  But, what happens if issues aren’t communicated in a timely manner?  What about the liabilities associated with missing your SLAs?

Twilio is a communications API platform company, that is always looking at ways to solve these problems through voice and messaging. One great way to solve this case management is through SMS, one of the most efficient and ubiquitous ways to communicate.  In this post, we’ll show you how to seamlessly integrate real-time SMS messaging into your Salesforce.com installation.

Situation: Trigger SMS alerts to Case Team Members for High Priority Cases

One of your customers has called to report a critical issue.  The call center agent creates a “high” priority case and assigns a case team to work on the issue.

Figure 1: Salesforce Case Management Screen

 

Creating the case fires a trigger that notifies the assigned team member(s) via SMS.  The entire team is simultaneously notified, enabling them to understand the issue and to start resolving it as quickly as possible.

Figure 2: Received SMS from Salesforce via Twilio

Let’s dive into the code…

 

Prerequisites

  1. To begin, you’ll need an account with Twilio and a Twilio phone number (requires login).
  2. You’ll also need a Salesforce account where you can do the development.
  3. Next, you’ll need to install the Twilio Helper Library for Salesforce into your Salesforce org.

 

Step 1:  Creating a trigger on Case Object

This trigger calls an Apex method, which has all the business logic to produce a list of case team members, and the message that needs to be communicated via SMS.

Create an “after insert” and “after update” trigger on Case.

trigger trg_case_send_sms on Case (after insert, after update) {
    //Call the Apex class to Send out SMS messages
    TwilioSMSAsync.sendSMSCaseTeamList(Trigger.newMap.keySet());
}

 

Step 2:  Creating the Apex method to send SMS message

The Apex method “TwilioSMSAsync.sendSMSCaseTeamList” has the business logic to determine when to send the SMS message.  In our use case, we want to send the SMS when the Priority of the case is ‘High’  (seen annotation 1 in Figure 1) and to the entire assigned case team (see annotation 2 in Figure 1).  The text message is completely configurable by the Force.com developer.  In the example code, we specify the Case Number, Case Contact Name, their Account Name and a URL for the SFDC case record in the SMS message.

Note: The method sendSMSCaseTeamList is specified as @future(callout=true) since it asynchronously makes external calls to Twilio REST APIs when the trigger fires.

The Apex method does three things in the following order:

  1. Gets the case details for creating the SMS message body
  2. Gets the list of case team members associated with the case
  3. Using the Twilio SMS API, sends a SMS to each of the case team member in real-time

 

public class TwilioSMSAsync {
  @future (callout=true)
  public static void sendSMSCaseTeamList(Set<Id> Ids) {

    String caseNumber = '';
    String caseContactName = '';
    String caseContactAccountName = '';
    String caseTeamMemberContactId = '';
    String contactName = '';
    String toNumber = '';
    String messageBody = '';
    String SFDC_hostnameUrl = URL.getSalesforceBaseUrl().toExternalForm();

    // Get Case Info
    for (Case c: [SELECT Id, CaseNumber, AccountId, ContactId, Priority FROM Case WHERE id IN :Ids]) {
       // Only send SMS to case team members if the Priority of the case is set to ‘High’
        if (c.Priority == 'High') {
            caseNumber = c.CaseNumber;
            for (Contact cc: [SELECT Name FROM Contact WHERE id = :c.ContactId]) {
                caseContactName = cc.Name;
            }
            for (Account cca: [SELECT Name FROM Account WHERE id = :c.AccountId]) {
                caseContactAccountName = cca.Name;
            }
	// Create the SMS message body
            messageBody = 'SFDC High Priority Case ' + caseNumber + ': ' + 'For ' + caseContactName + '@' + caseContactAccountName + ' '  + SFDC_hostnameUrl + '/' + c.Id ;

            // The TwilioAPI helper class looks up your Twilio AccountSid and AuthToken from your current organization, in the TwilioConfig custom setting.
            // You can configure TwilioConfig by going to Settings->Develop->Custom Settings->Twilio_Config, and your AccountSid and AuthToken
            // can be found on the Twilio account dashboard
            TwilioRestClient SMSclient = TwilioAPI.getDefaultClient();

            // Iterate through all the Case Team Members and send them a SMS notification
            for (CaseTeamMember ct : [SELECT MemberId FROM CaseTeamMember WHERE ParentId = :c.Id]) {
                caseTeamMemberContactId = ct.MemberId;
                for (Contact cc: [SELECT name, MobilePhone FROM Contact WHERE id = :caseTeamMemberContactId]) {
                    contactName = cc.name;
                    toNumber = cc.MobilePhone;
                    // Format (+<Country Code><Number>) the toNumber
                    toNumber = '+' + toNumber.replaceAll('\\D', '');

                    // Setup the params for SMS message
                    Map<String,String> params = new Map<String,String> {
                        'To'   => toNumber,
                        'From' => '<You_Twilio_Provisioned_PhoneNumber_Goes_Here>',
                        'Body' => messageBody
                    };
                    //Send SMS out via Twilio
                    TwilioSMS sms = SMSclient.getAccount().getSmsMessages().create(params);
                }
            }
        }
    }
  }
}

Summary

Now, you have a working example of sending SMS in real-time to case team members, enabling the service support team to respond to critical customer issues effectively.   Since the Twilio REST API helper library is written natively in Force.com, it’s easy to integrate with other Salesforce Objects or Case Management applications like ServiceMax.

In future posts, I’ll show you how to extend this further so your case team members can respond to the SMS message, essentially, creating a bi-directional SMS messaging system.

Twilio provides the tools to solve a wide range of communications needs.  For instance, with Twilio’s Force.com helper library, we can implement:

  • Click to call: Easily integrate Click2Call within Site.com or any website
  • Conferencing: Enable Conference calling within Salesforce
  • Interactive Voice Response (IVR) System: Build context aware IVR systems based on Salesforce data

 

tagged , , , , , , , , , , , , , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.