fbpx

Twilio Flex with Salesforce Integration Tutorial

Twilio for salesforce
16 March, 2021 5 min to read

In this guide, you will learn how to configure Flex within Salesforce, configure outbound calling, enable SSO (Single Sign-On), and launch your integration.

With out-of-the-box support for Salesforce Open CTI, you can now power your Salesforce environment with omnichannel communications offered by Flex. Both Salesforce Classic and Lightning Experience integrations are supported, allowing you to use Flex directly within your Salesforce instance.

With the example below, you can create a custom Salesforce IVR logic to handle all incoming calls by your agents, view caller information (Lead/Contact records will be automatically opened for an incoming call).  Behind the scenes, your Twilio Salesforce connection will create new tasks from inbound calls and missed calls with call/voicemail recordings.

Step-by-Step Guide: Twilio Flex Setup

Use this guide to create default Twilio Flex Connection with Salesforce. Follow the link below to create default Twilio Flex ↔ Salesforce integration

https://www.twilio.com/docs/flex/admin-guide/integrations/salesforce

Salesforce Integration Configuration

This example shows how to configure your Twilio Flex and download Call Center Definition file

Fill up all required fields as shown below and click Download.

The downloaded file will contain all required data in yaml format and be ready for further insert at Salesforce > Setup > Call Center > Call Center Definition File section.
Salesforce Integration Configuration first step
Salesforce Integration Configuration parameters

Twilio Configuration

Active Numbers

First of all, you need to buy the number of phone numbers required for your future Twilio for Salesforce logic and configuration.

In the image below, there are two numbers used for different purposes to handle our general logic.
Configuration of Active Numbers
One is used to handle inbound calls via IVR (Salesforce IVR) and the second is used to as "redirect to SIP" if no agents are available before the call will be processed to Recording Voicemail.

Configure phone numbers

For SIP (Bria)
Configure phone numbers For SIP (Bria)
For Salesforce IVR
Configure phone numbers For Salesforce IVR

Task Router

https://www.twilio.com/docs/taskrouter/lifecycle-task-workflows-and-assignment

TaskRouter's primary job with a pending Task is to find a matching Worker. This assignment is handled by the Workflows within a Workspace.

When created, every Task is associated with a Workflow that is used to evaluate how to find a matching Worker. These Workflow configurations, represented in JSON, trace the path a Task takes until either a Reservation is accepted or the Task is canceled.

Settings

TaskRouter's settings
Task Router settings reservation is accepted

TaskQueues

Here we need to create different Queues where the caller will be transferred via IVR.
TaskQueues creation
"Agents" queue will show as available (logged in) users. Here we are defining a rule to filter users.
"Agents" queue
Add another queue "Voicemail" to record a voicemail from the caller and send it out to Salesforce
record a voicemail from the caller

TwiML Bins

So, the logic here is:

  1. When we receive a call in IVR it processes the caller via some logic of inputs and outputs and then tries to connect it with available agents.
  2. IF no agents are available, it redirects the call to Bria via TwiML Bin.

redirection a call to Bria via TwiML Bin
configuration of redirection to Bria via TwiML Bin

Workflows

In this section, we need to define how our IVR should handle inbound and outbound calls.how IVR should handle inbound and outbound calls

Inbound call rule

Inbound call rule

Outbound call rule

Outbound call rule

Functions

Twilio Flex will create default functions to apply its functionalities, however, we need to add some custom logic.
adding a custom logic to Twilio Flex Functions
Create the "Available Agents" function.

Available Agents

 

Create another function "Connect to Salesforce" which will be used to send inbound call data to Salesforce > Platform Event

Connect to Salesforce

 

Create or adjust the "Salesforce integration Outbound start" function.

 

Salesforce integration Outbound start

Configuration

Set up your environment variables to be available to use inside functions.
Set up environment variables
NOTE

Those settings must be changed depending on your production/sandbox environment. To handle this, create services  for each instance and define all the variables and functions, that you need for your Salesforce Twilio custom integration.

Studio

Salesforce IVR - is used to combine all defined settings above and process our inbound call via logic. It has several branches depended on the caller's input.
Studio Salesforce IVR

  1. If the user when asked didn't provide any input then we will address him/her to a voicemail, but before we'll ask him to provide feedback again.
    logic if user didn't provide any input
  2. If the user has pressed 1 as was asked by IVR, then it will check for available agents and if present then connect by sending to Salesforce Caller details and start recording the call.
    logic if the user has pressed 1
    sending to Salesforce Caller details
  3. If currently there are no available agents in the "CheckResponse" block, then it will try to connect the caller with SIP (Bria).
    If currently there are no available agents
    If there is no answer from SIP
  4. If there is no answer from SIP or the call was dropped by the recipient, it will proceed to a Voicemail. After voicemail is recorded and the call is completed, then the data will be sent to Salesforce and create a new Task with a link to the recorded message.

new Task with a link to the recorded message

Salesforce Customs

Custom Object

  1. Create a new Custom Object - this object is needed to store all Twilio Call Recordings Id's.
    CallRecording CallRecording__c
  2. Create new custom fields inside the custom object

Creation of new custom fields inside the custom object

Call Center

Add users who will be eligible to use this Call Center
Adding a users who will be eligible to use Call Center

NOTE
This assumes that you have followed the link at the start of this topic and configured your CallCenter with the downloaded CallCenter Definitions file from Twilio Flex > Integration > Configuration.

Connected App

  • Create new Connected App Twilio Flex
    Create New Connected App Twilio FlexCreate New Connected App Twilio Flex setup
  • Create new Connected App Twilio APICreate new Connected App Twilio APICreate new Connected App Twilio API setup
  • Connect Twilio with Salesforce using created SSO

Permission Set

Create a new permission set that will allow us to communicate via Salesforce API
Creation of a new permission set

Asking permission set to users which will use Twilio CallCenter

Asking permission set to usersPlatform Event
This is our endpoint in Salesforce needed to handle custom requests from Twilio API. (Example of the endpoint: https://{your_domain}.salesforce.com/services/data/v48.0/sobjects/TwilioEvents__e/)

  • Setup > Platform Events > New Platform Event
  • Give your own name or use as per image
    Platform Event setup
  • Add custom fields (*if required) - Custom Fields & Relationships section

Apex Trigger

TwilioCallTrigger

This trigger will process requests received on our TwilioEvents (Platform Event).
Logic

  • This trigger starts on the new Platform Event action.
  • It will verify if the request contains the agent name (custom logic) and use it to get the User ID in our Salesforce platform or set a default user.
  • Check if the given subject is not "Missed Call"
  • Check if CallSid is not blank
  • Check if the event contains RecordSid
  • If true - it will create a new task assigned to an agent with a defined Call and Record Sid's
  • Or - it will create a new task assignee to an agent but will process some other custom logic trying to retrieve recorded calls from CallSid

TwilioCallTrigger

 

Task Trigger

 

This trigger will try to obtain call recordings from CallSid given in the description from our TwilioCallTrigger. We separate logic in the different process because if using @future callouts
Logic

  • Check if the task "Description" field is not empty
  • Split description into the array as its data stores as a string in SF object
  • Find CallSid and store it in the separate custom field
  • If CallSid exist retrieve a recordings data

TaskTrigger

 

CallRecordings Trigger
Logic
  • On creation or update of CallRecordings
  • Find task with stored CallSid
  • Set recordings links to task
  • Update task

TaskUpdate

 

Apex Helper Class
  • Create new Apex Class for your TwilioCallTrigger

TwilioRest

 

Apex Test Classes
  • TestDataFactoty Class

TestDataFactory

 

  • TestTaskTrigger Class

TestTaskTrigger

 

  • TestTwiliEvents Class

TestTwilioEvents

  • TestTwilioCalloutMock Class

TwilioCalloutMock

  • TestTwilioRest Class

TestTwilioRest

https://www.twilio.com/docs/salesforce

Share with:

get in touch

Need to speak with our experts?

Fill in your personal details

     Terms and Privacy Policy