Improving Your Case Management Response Time with Twilio SMS

May 14, 2013
Written by
Ameer Badri
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

ameer
Ameer is Solutions Architect at Twilio, specializing in building high volume, low latency solutions for customers. He has over 15 years of experience in building solutions. The following is syndicated content, originally published on Salesforce’s developer blog.

When it comes to case management, the shorter your response time is the happier your customers will be. I recently published an article on Salesforce’s development blog, showing you how to improve your case management response time using a Twilio SMS integration.

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.

SFDC-Case1039-Details

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.

Received-SMS-from-SFDC

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.

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:

a) Gets the case details for creating the SMS message body
b) Gets the list of case team members associated with the case
c) 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 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 (+) 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, we’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.

Find out more about Twilio solutions here