Menu

Expand
Rate this page:

Push Inbound Messages and Status Updates to Salesforce (Deprecated)

Since version 4.65, Twilio for Salesforce automatically syncs your messages to Salesforce. However, you must tell Twilio how to handle inbound messages — if you do not, your incoming messages may be marked as Failed Error:30008 by Twilio and not synced. To learn how to avoid this, please see our guide to Receiving Incoming Messages in Salesforce.

The following method uses a Twilio function to sync messages to Salesforce. Use the following method only if you are using an old (ie. pre 4.65) version of app, or have been instructed by Twilio to do so, due to your specific use case. If you are an existing customer, you may have this already configured. In this case, your message sync will keep on working as expected without any need for modification.

Many of the events that occur during a message lifecycle, such as delivered or failed, are asynchronous. Twilio typically sends these events as a POST request to a webhook that you specify in the Console. Status updates tell you which messages have succeeded or failed.

For Salesforce, we use a Connected App and a Twilio Function to authenticate and post data back into Salesforce. The chain of events is as follows:

  1. One of your phone numbers receives a new message or message status update.
  2. The messaging service (your number pool) invokes a Twilio Function.
  3. Your Twilio Function authenticates with Salesforce using credentials from a Connected App.
  4. Salesforce creates a new message object (for inbound messages) or updates an existing message with a status.

Flow diagram for Twilio and Salesforce interaction

Setting up message status updates should take about 20 minutes. By the end of the setup process, you will have message status data captured in Salesforce. We’ll finish with a report on our messages that shows which were delivered or were not delivered.

Create a Salesforce API User

We strongly recommend creating a Salesforce API User to use with your Connected App as a good security practice. In the event that your Connected App credentials are exposed, an API user will limit an attacker’s ability to access your organization’s user interface and act as an administrator. To create a Salesforce API User, follow Salesforce’s documentation. In summary, the steps are:

  1. Create a normal user with appropriate licenses. You may also consider giving them a new specific profile.
  2. Go to Setup > Manage Users > Profiles.
  3. Open the profile of the new User. Click Edit.
  4. Scroll down to Administrative Permissions and check the API Enabled box.

Any team member — developer or administrator — that you add to your Twilio Console project will be able to view your API User’s credentials. We recommend restricting Twilio Console access to only the necessary team members and enabling 2FA for your Twilio account.

1. Create a Salesforce Connected App

  1. Use Salesforce to create a new Connected App.
    • In Lightning Experience, you use the App Manager to create Connected Apps: From Setup, enter App in the search box, then select App Manager. Click New Connected App.
    • In Salesforce Classic, from Setup, enter Apps in the search box, then select Apps (under Build | Create). Under Connected Apps, click New.
  2. Enter the name of your application.
  3. Enter the contact email information, as well as any other information appropriate for your application.
  4. Select Enable OAuth Settings.
  5. Enter https://www.twilio.com as the Callback URL.
  6. Find Access and manage your data (API) under Available OAuth Scopes and add it to Selected OAuth Scopes:
    Create a Salesforce Connected App with Twilio
  7. Click Save. You will see an alert that the connected app could take 2–10 minutes for the changes to take effect. Just click Continue:
    Connected App success messaging in Salesforce
  8. The Consumer Key is created and displayed, and the Consumer Secret is created — click the link to reveal it:
    Connected App Credentials.png

That’s it! you have successfully created your Salesforce Connected App. Now continue to the next step.

2. Create a Twilio Function

We’ll now create a Twilio Function to authenticate with our Salesforce Connected App. This will allow us to send data to Salesforce whenever we receive an inbound message or status update.

We’ve already created a Function template for you to use, so there’s no coding necessary. Just add the values from your connected app with your Salesforce login information.

  1. In a new browser tab, visit the Functions page in the Twilio Console.
  2. Click on the red + button to add a new function.
  3. In the New Function modal that opens, click on Connect to Salesforce:
    Choose Template.png
  4. Next, you’ll see a list of configuration variables for your function. Fill them out with the keys specified in the table below:
    Specify Variables.png

    For the user associated with SF_PASSWORD, SF_USERNAME, and SF_TOKEN, use the credentials that you generated when creating your API user.

Important If you are performing this task in a production Salesforce organization, make sure that you provision a Twilio for Salesforce license to the user whose credentials are being associated here. The setup guide describes how to provision licenses.

Key Value
SF_IS_SANDBOX Set to true if you’re testing in a sandbox organization (your organization’s URL starts with test.salesforce.com). Otherwise set to false.
SF_CONSUMER_KEY The Consumer Key from your Connected App.
SF_CONSUMER_SECRET The Secret Key from your Connected App. Click the link in Salesforce to reveal your Secret Key.
SF_PASSWORD

A Salesforce Password. This can be a personal password, but we recommend using a system user’s username.

If you are performing this task in production, make sure that this user has a Twilio for Salesforce license assigned to them.

SF_USERNAME

A Salesforce Username. This can be a personal username, but we recommend using a system user’s username.

If you are performing this task in production, make sure that this user has a Twilio for Salesforce license assigned to them.

SF_TOKEN

A Salesforce Security Token. This can be a personal token, but we recommend using a system user’s token.

If you have forgotten the token, you will need to reset it. Visit the salesforce instructions for details.

Once you've added your configuration variables, click the red Create button. You will be directed to a Function page that looks like this:

Twilio function example to push SMS status updates to Salesforce

3. Connect your Twilio Function to the Messaging Service

Finally, ensure that your Twilio Function runs whenever your messaging service (pool of numbers) sends a status callback or receives an inbound message.

  1. In your Twilio Function from Step 2 above, copy the function path (your function’s unique URL):
    Copy Function Link.png
  2. In a new tab, go to the Messaging Services page in your Console.
  3. Open the Messaging Service that you created during your initial set up, and go to the page’s Outbound Settings section.
  4. Paste your Twilio Function URL in two places:
    1. As the Request URL input under Inbound Settings.
    2. The Status Callback URL input under Outbound Settings.
  5. Click Save at the bottom of the page:
    Paste Function Links.png

4. Test and verify

Note Salesforce Connected Apps can take 2–10 minutes to provision. If you try to test your function immediately after you provision the Connected App and receive an authentication error, wait 10 minutes and try again.

To test that your Function is working properly, open your Function page (click on your Function from the list). Now try sending an SMS to your Salesforce phone number.

If you’ve successfully set up your Function, you should see the following log messages, including “Success Getting Token”:

Successful Function.png

Common pitfalls

Error Solution
I didn't see any logs when I texted my number. Nothing happened. Check that you have the correct links set in Step 3. Be sure to click Save on your Messaging Service page to update your configuration.
Error Getting Token: {"error":"invalid_grant","error_description":"authentication failure"} This error indicates an issue with your login credentials. Check that the values of the variables SF_IS_SANDBOX, SF_USERNAME, SF_PASSWORD, and SF_TOKEN entered in Step 2 are correct.
Error Getting Token:{"error":"invalid_client_id","error_description":"client identifier invalid"} Your function is successfully authenticating with your username and password, but your SF_CONSUMER_KEY is not correct. It should be 85 characters long, including letters, numbers, and symbols.
Error Getting Token:{"error":"invalid_client","error_description":"invalid client credentials"} Check that your SF_CONSUMER_SECRET is correct. It should be a 19 digits long and only include numbers.

5. Report on your messages

Now that you’ve set up your status callback system, you can create a report on the status of all of our messages.

  1. In the Reports tab, create a new report.
  2. Click on Other Reports then select Messages as the object to report on:
    Other Reports
  3. Click Continue.
  4. Add the Body and Status columns to your report:
    MessageReport
  5. Finally, try sending some messages to make sure you’re getting updates. Text your own phone for a successful delivery, then try creating a contact with one of Twilio’s test numbers to see an undelivered status.

Wrap Up

Congratulations! You've now successfully captured message deliverability within Salesforce. Now that you have a Twilio Function set up, you can re-use this pattern to post data into Salesforce.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more