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.
Table of Contents
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.
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.
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)
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
TaskQueues
Here we need to create different Queues where the caller will be transferred via IVR.
"Agents" queue will show as available (logged in) users. Here we are defining a rule to filter users.
Add another queue "Voicemail" to record a voicemail from the caller and send it out to Salesforce
TwiML Bins
So, the logic here is:
- 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.
- IF no agents are available, it redirects the call to Bria via TwiML Bin.
Workflows
In this section, we need to define how our IVR should handle inbound and outbound calls.
Inbound call rule
Outbound call rule
Functions
Twilio Flex will create default functions to apply its functionalities, however, we need to add some custom logic.
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.
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.
- 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.
- 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.
- If currently there are no available agents in the "CheckResponse" block, then it will try to connect the caller with SIP (Bria).
- 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.
Salesforce Customs
Custom Object
- Create a new Custom Object - this object is needed to store all Twilio Call Recordings Id's.
CallRecording CallRecording__c - Create new custom fields inside the custom object
Call Center
Add users who will be eligible to use this 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 API
- Connect Twilio with Salesforce using created SSO
Permission Set
Create a new permission set that will allow us to communicate via Salesforce API
Asking permission set to users which will use Twilio CallCenter
Platform 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
- 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