Send SMS From Salesforce using Twilio

Combine the power of Salesforce and Twilio to send critical SMS alert from your salesforce Org. Join us and learn about how to configure Twilio in Salesforce and send SMS from Salesforce using Twilio.

What is Twillo

Twilio is a customer engagement platform. It helps organizations and brands to have a better engagement and communication with users. You can read more about Twilio at their official web site.

Twilio has a exclusive app for Salesforce. You can use this app and can integrate the power of Twilio with Salesforce. There can be very useful use cases where we can integrate Twilio with salesforce. Few of them are –

  1. Sending welcome text to the users.
  2. Sending any critical alert to admin.
  3. Sending alert to the account manager when a case get escalated.
  4. Sending reminders text.

There are numerous examples where we can use Twilio in salesforce. We can send customized and automatic SMS from process builders, Flows, record pages and also from REST API.

For this article, I have picked up a very basic use case that once a case gets created in the salesforce org and the case priority is High, send an SMS alert to the contact of that case.

Before we get started on our solution, we need to perform following tasks to get the Twilio up and running and also connected with our salesforce org.

  1. Sign up with Twilio , you can sign up for a trial account.
  2. Configure Twilio in Salesforce org.
  3. Build the Salesforce solution to integrate Twilio. We will create a record triggered flow for this article. You can also build process builder (I know at some point of time, process builder will go away) or you can also use Apex classes.
  4. Test your solution

Setup Twillo

first setup is setup the Twilio. Let see how we can setup Twillo

1 . Sign up with Twilio :

First steps is, we need to have a Twilio account set up. You can have a free trial account created and use that for demo purpose. You need to provide few basic details along with your email ID and phone number.

2.  Install Twilio in your sandbox:

After you have successfully signed up, you will get an option to install Twilio in your salesforce org.

Once you login to your org with the user name and password. You need to choose that do you want to install it only for admin or for all users. This is same as installing any other app/package from app exchange so nothing special.

Please note Twilio creates few components in your org e.g. It installs few Apex classes, permission sets, objects etc.

3. Get a Phone Number from Twilio and set up your account:

Don’t worry, you need to get a subscription for that but you will get a phone number immediately from your Twilio account. That phone number will be used for messaging service.

You will also need to set up a messaging service in your account. You will need this messaging service name in salesforce while setting up your flow.

Please refer below image for more information. Since I already got a trial phone number , so in my screen you will see that you already have a trial phone number.

Once you get a phone number, create a message service in your account. You just need to provide service name. That service name you will use later while building a solution in salesforce org.

4. Take a note of your Twilio account details:

Once your are in your account, you will see your account details. Two important things to note is – 1. Account ID 2. Token.

These two things, you will need in Salesforce while establishing connection between the two platforms. So please take a note of these. These are like username and password.

5. Configure Salesforce to use Twilio account:

In Step #2, we have installed the Twilio package in salesforce and now its time to check few things.

Imp things which we need to check

  • Check that user has the permission for required Twilio permission sets before using Twilio. If not, assign below permission set to users:
Permission set for twillo
  • Check that Twilio App is assigned to the user. If not please assign that. You need it for set up and configure the app.
  • Configure the Twilio account in Salesforce org:

From the app launcher select Twilio configuration. If you have done all above steps correctly and if the admin user which you are using for this configuration has the right permissions he will see below screen. If not, then please check that you haven’t miss any configuration for the user.

Here you need to enter AccountID and authentication token as noted above and validate the credentials.

Now we have successfully set up the Twilio app in our salesforce org and ready to use the functionality provided by it.

There can be multiple use cases for Twilio SMS alert but the one which I picked up for this article is, if a High priority case gets created then send a text alert to the contact person and notify him about the case by sending a text message on his mobile phone.

Create a Flow to send SMS

As per our use case, when a “High” priority case gets created, send SMS alert to the contact of that account, if mobile number exists and if mobile number does not exists then send an email.

Below are the complete description of the flow-

A. Initiate a record triggered flow , when a case object is created with Priority as “High”.

B. Use the Get Record element to get the contact ID for the created case, store the contact ID in a variable. To refer the created case (record who triggered the flow), you have to use $record Global variable.

C. Use Get Record element to query the contact object to store contact’s email ID and Mobile number in respective variables for the contact ID (stored in a variable in above step #B).

D. Next, I have used a decision box to check, if the mobile number is not null for that contact.

1. If mobile number is present, send text alert (Details of that action given below)

2. If Email ID is present and mobile number is not present then send email.

Please take a look at the pictorial view of complete Flow diagram –

All other elements which are used to create this flow are the regular flow element besides Apex Action to send SMS. So lets dive deeper and understand that –

Apex Action (Send Text): –

Drop an apex action in your flow canvas and configure it. If you filter the action by Type and search Twilio, you will see an action with the name Twilio send SMS, select and configure it. You need to provide what you want to send (i.e. text message content) and where you want to send (i.e. mobile number). We have already stored contact mobile number in a variable and that we will use in below action configuration.

That’s it and you are all set to test and activate this flow. Thank you for reading.

Gaurav Jain
Gaurav Jain

I have more than a decade of experience in IT industry and played multiple roles in my career, starting from Software Developer, trainer , Business analyst and Technical manager. I have worked on many technologies which includes ASP.net, C#, WCF, SSIS, SQL Server , Salesforce , Apex etc. I am a Microsoft and Salesforce Certified Developer and I love writing technical blogs.

Articles: 3

2 Comments

  1. Hi Apex Hours, Can we send the SMS notification to Case Owner Queue members, where the owner is a Queue. We want to notify to all Queue members when the Case priority is High.

  2. In Debugging Details it shows like this i haven’t receive message to my number,

    TWILIOSF__TWILIOSENDSMS (APEX): Twilio Send SMS Message
    Inputs:
    message = {!Enter_Donor_Name} (Rohit)
    toNumber = {!Phone.value} (8148804734)
    Outputs:
    None.

Leave a Reply

Your email address will not be published. Required fields are marked *