Menu

Rate this page:

Thanks for rating this page!

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

Send SMS Messages from Process Builder

Do you work for a nonprofit or social enterprise? Take advantage of discount pricing, expert help, and $500 in free API credits by signing up for the Twilio.org Impact Access Program.

Process Builder is a popular tool for building automated workflows in Salesforce. Twilio for Salesforce exposes two global Apex classes: Twilio Send SMS and Twilio Send to Studio that you can use to send out automated SMS messages when certain conditions are met. Using Process Builder, you can build appointment reminders, chat bots, SMS notifications, and many other common Twilio use cases.

Twilio Send SMS Message allows you to send a text message from Process Builder. This Apex class is useful for single message use cases such as alerts and notifications.

Twilio Send to Studio allows you to initiate SMS and Voice chatbots using Twilio Studio, Twilio's drag-and-drop visual editor. This Apex class is useful for initiating longer conversations that might require if/then branching.

Send SMS messages with Process Builder and Twilio for Salesforce

Ready to get started with sending simple SMS alerts? Follow the instructions in the video below.

Twilio Send SMS Message Parameters:

Parameter Description
Message The body of the SMS message that you would like to send. This can be static, a field reference, or a formula (useful for templated messages). Note the message length determines the number of segments.
To Mobile Number The phone number you want to send the message to. This can be static, a field reference, or a formula.

Send to Studio with Twilio for Salesforce

Interested in crafting interactive bot conversations with your users? Learn how you can launch SMS bots using Twilio Studio and the Salesforce Process Builder in this video:

To configure the Twilio Send to Studio Apex class, provide the following values:

Parameter Description
From Number The phone number you want the Studio flow to be sent from. Currently we only support sending from a specific number, not a messaging service. This might mean your chat bot will contact a user from a new number.
Studio URL The Rest API URL that will call your Studio flow. Example: https://studio.twilio.com/v1/Flows/FWXXXXXXXXXXXXXXXXXXXXXXXXX/Executions. To find your Studio flow API URL, click on the gear icon in the top-right corner of the flow's trigger.
To Mobile Number The number that the chat bot will send messages or make calls to.
Parameters (Optional) You can include additional Salesforce data as context parameters in key/value pairs. This is useful if you want your chat bot to address the recipient by first name, or reference other information from Salesforce within your Studio flow under {{flow.data.your_param_key}}.

Updating a Salesforce Record with a Twilio Function

The following code sample showing how to create a Salesforce contact using a Twilio Function. Functions can be run in Twilio Studio using the Function widget. Parameters passed to the function widget will be accessible on the `event` object.

Note: Before running this function, you will need to add "nforce" as a package dependency in your functions configuration page:

add-package.png

exports.handler = function(context, event, callback) {
    console.log('Running function')

    let nforce = require('nforce');
    let response = new Twilio.Response();
    let oauth;
    
    // Create the connection with the Salesforce connected app
    let org = nforce.createConnection({
      clientId: context.SF_CONSUMER_KEY,
      clientSecret: context.SF_CONSUMER_SECRET,
      redirectUri: 'https://twilio.com',
      mode: 'single'
    });
    
    // Authenticate and return OAuth token
    org.authenticate({
      username: context.SF_USERNAME,
      password: context.SF_PASSWORD + context.SF_TOKEN
    }, function(err, resp){
        
        if (!err) {
          console.log('Successfully authenticated:', resp);
          oauth = resp;
        }
        
        let lastname = `${event.first_name} ${event.last_name}`; 
      
        // Set contact data
        let ct = nforce.createSObject('Contact');
        ct.set('lastname', lastname);
        ct.set('State__c', 'California');
        ct.set('Phone', event.phone);
        ct.set('MobilePhone', event.phone);

        // Create contact in our SFDC org
        org.insert({ sobject: ct, oauth: oauth }, function(err, resp){
          if(!err) {
              console.log('Successfully created contact');
              callback(null, null)
          } else {
              console.log("Error creating contact:", err);
          }
        });
    });
};

For more examples, see the documentation for nforce, the NodeJS client library for 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.