1. Home
  2. Docs
  3. Tutorials
  4. Connect your conversational app with your Salesforce CRM

Connect your conversational app with your Salesforce CRM

Salesforce

You need CRM to make your business more efficient and increase your revenue by creating repeatable successful processes. Your Salesforce CRM would be a great addition to your conversational applications. Your Amazon Alexa skills, Google Home actions, and Facebook messenger bots could be a great source of customer data and can utilize the data you collect to provide a better service to your customers.

In this tutorial, we will learn how you can update your Salesforce CRM data directly from your conversational application, how we can fetch the user data from Salesforce CRM and use it to personalize the response. We will demonstrate how the application can react when the users ask to get in touch with the sales department. The application will try to identify the user in the CRM, will create a new lead if needed, and if possible, will offer to connect the user immediately to an agent.

Some of the information in this guide is more developer-centric, but don’t be discouraged. If you follow the steps as they’re outlined, you will have a deployed, working “connect me to sales” app.
You can download this tutorial code from here. Later just import it to a new app suing the Import button.

Create new intents

  1. Add a new intent named “ContactSales”
    • Add the sample “Please ask sales to contact me” to the “User says”
    • Add the following entities:
      • “FirstName” – “US First Name”
      • “Email” – “Email”
      • “PhoneNumber” – “Phone Number”
    • Add a script response with the following code. We will add the code later to the “business logic” tab.
      return ContactSales(context, req, res);
      
  2. Add a new intent named “EnterEmail”
    • Add the samples to the “User says”:
      • “(|My email is|My address is|My email address is) {Email}”
    • Add the following entities:
      • “Email” – “Email”
    • Add a script response with the following code.
      return sdk.conversationHelper.FillEntity([
       {
       "name":"Email"
       }], context, req, res);
      
  3. Add a new intent named “EnterPhoneNumber”
    • Add the samples to the “User says”:
      • “My (phone|mobile|telephone|) number is {PhoneNumber}”
      • {PhoneNumber}
    • Add the following entities:
      • “PhoneNumber” – “Phone Number”
    • Add a script response with the following code.
      return sdk.conversationHelper.FillEntity([
       {
       "name":"PhoneNumber"
       }], context, req, res);
  4. Add a new intent named “EnterFirstName”
    • Add the samples to the “User says”:
      • (|my first name is|my name is) {FirstName}
    • Add the following entities:
      • “FirstName” – “US First Name”
    • Add a script response with the following code.
      return sdk.conversationHelper.FillEntity([{
              "name": "FirstName"
          }], context, req, res);
  5. Add a new intent named “EnterFullName”
    • Add the samples to the “User says”:
      • (|my name is) {FirstName} {LastName}
    • Add the following entities:
      • “FirstName” – “US First Name”
      • “LastName” – “US Last Name”
    • Add a script response with the following code.
      return sdk.conversationHelper.FillEntity([{
              "name": "FirstName"
          },
      {
              "name": "LastName"
          }], context, req, res);
To make sure you properly identify the users’ intent, you need to configure 10-30 different samples.

Business logic

The code below will collect the needed lead information from the user, will track the lead’s activity in Salesforce CRM, and will offer the lead to get a phone call from an agent. If the user agrees, the system will initiate a phone call immediately.

function ContactSales(context, req, res) {
    return GetLeadInfo(context, req, res, ["phoneNumber", "firstName"]).then(function(leadInfo) {
        if (!leadInfo) {
            return;
        }
        req.getSession().set('onYes', JSON.stringify({
            callback: 'CallSales',
            data: {
                phone: leadInfo.phoneNumber,
            }
        }));
        req.getSession().set('onNo', JSON.stringify({
            callback: 'whatHappenedTodayOnNo',
            data: {}
        }));
        res.say("Do you want me to connect you now on the phone?");
    }).catch(function(err) {
        res.say(err);
    });
}

function CallSales(context, req, res, data) {
    sdk.twilloCall(data.phone, "+16467080983");
}
  • The function “GetLeadInfo” (we will see the details later) will get the lead’s phone number and first name
  • We will ask the user if he wants to get connected by an agent on the phone. You can learn more about developing a contextual application here.
  • If the user says “Yes” the system will initiate a call using the “sdk.twilloCall” method.

Collect user information

The function GetLeadInfo, will make sure to collect all the needed properties from the user, or from saved data in Salesfortce CRM. The key for each lead is its email, so we will start with collecting that. Notice that once we have some lead information we will save it in the session file, to make sure we don’t need to ask the user for same data again and again.

function GetLeadInfo(context, req, res, properties) {
    var origLeadInfo = {};
    var leadInfo = {};
    if (req.getSession().get("LeadInfo")) {
        leadInfo = JSON.parse(req.getSession().get("LeadInfo"));
        origLeadInfo = JSON.parse(JSON.stringify(leadInfo));
    }
    var wait = [];
    if (!leadInfo.email || leadInfo.email === "") {
        if (!req.slot("Email")) {
            sdk.conversationHelper.askForEntity(context, req, res, [
                [{
                    "name": "Email",
                    "target": "Email"
                }]
            ], "What is your email address?", "For example you can say, My email address is john.smith@gmail.com");
            return Promise.resolve(false);
        }
        leadInfo.email = req.slot("Email");
    }
    return GetLeadFromCRM(context, req, res, leadInfo.email).then(function(result) {
        if (result && result.firstName && result.firstName !== "") {
            leadInfo.firstName = result.firstName;
        } else if (req.slot("FirstName")) {
            leadInfo.firstName = req.slot("FirstName");
        }

        if (result && result.lastName && result.lastName !== "") {
            leadInfo.lastName = result.lastName;
        } else if (req.slot("LastName")) {
            leadInfo.lastName = req.slot("LastName");
        }

        if (result && result.phoneNumber && result.phoneNumber !== "") {
            leadInfo.phoneNumber = result.phoneNumber.replace(/\D/g, '');
        } else if (req.slot("PhoneNumber")) {
            leadInfo.phoneNumber = req.slot("PhoneNumber").replace(/\D/g, '');
        }

        if ((properties.indexOf("firstName") != -1) && (!leadInfo.firstName || leadInfo.firstName === "")) {
            sdk.conversationHelper.askForEntity(context, req, res, [
                [{
                    "name": "FirstName",
                    "target": "FirstName"
                }]
            ], "What is your name?", "For example you can say, my name is John Smith.");
            return Promise.resolve(false);
        }
        if ((properties.indexOf("lastName") != -1) && (!leadInfo.lastName || leadInfo.lastName === "")) {
            sdk.conversationHelper.askForEntity(context, req, res, [
                [{
                    "name": "LastName",
                    "target": "LastName"
                }]
            ], "What is your last name?", "For example you can say, my last name is Smith.");
            return Promise.resolve(false);
        }
        if ((properties.indexOf("phoneNumber") != -1) && (!leadInfo.phoneNumber || leadInfo.phoneNumber === "")) {
            sdk.conversationHelper.askForEntity(context, req, res, [
                [{
                    "name": "PhoneNumber",
                    "target": "PhoneNumber"
                }]
            ], "What is your phone number?", 'For example you can say, my phone number is <say-as interpret-as="telephone">6467080983</say-as interpret-as>.');
            return Promise.resolve(false);
        }
        if (JSON.stringify(leadInfo) !== JSON.stringify(origLeadInfo)) {
            UpdateCRM(leadInfo);
        }
        req.getSession().set("LeadInfo", JSON.stringify(leadInfo));
        return Promise.resolve(leadInfo);
    });
}
  • We will start by looking for saved lead information. You can read more about storing and getting session data here.
  • If we do not have any stored email information, we need to ask the user to provide us with that information first. We collect that information using the method “sdk.conversationHelper.askForEntity” You can learn how to collect information from your user here.
  • Once we have the user’s email address, we will use it to get the CRM data using “GetLeadFromCRM” (we will see the details later)
  • Now that we have the CRM data we will see if we are missing some of the needed properties. If something is missing, we will collect it from the user.

Connecting to Salesforce CRM

You can connect to Salesforce CRM using the integrated Node.js jsforce module.

To initialize the Salesforce module, call the following code. Notice – You must use your own username and password.

var jsforce = require('jsforce');

const userName = "";
const password = "";

Fetching lead data from Salesforce CRM

We will use the method conn.query to fetch the CRM data. Notice that we will store the CRM data in the session to avoid multiple calls to Salesforce.

function CRMtoLeadInfo(result) {
    var leadInfo = {};
    try {
        if (result.Id && result.Id !== "") {
            leadInfo.id = result.Id;
        }
        if (result.Email && result.Email !== "") {
            leadInfo.email = result.Email;
        }
        if (result.FirstName && result.FirstName !== "") {
            leadInfo.firstName = result.FirstName;
        }
        if (result.LastName && result.LastName !== "") {
            leadInfo.lastName = result.LastName;
        }
        if (result.Phone && result.Phone !== "") {
            leadInfo.phoneNumber = result.Phone;
        }
    } catch (err) {
        console.error(err);
    }
    return leadInfo;
}

function GetLeadFromCRM(context, req, res, email) {
    if (req.getSession().get("CRM")) {
        var crmInfo = JSON.parse(req.getSession().get("CRM"));
        return Promise.resolve(CRMtoLeadInfo(crmInfo));
    }
    var conn = new jsforce.Connection();
    return conn.login(userName, password).then(function(userInfo) {
        return conn.query("SELECT Id, FirstName, LastName, Phone, Email  FROM Contact WHERE Email='" + email + "'").then(function(result) {
            if (!result || !result.records || result.records.length === 0) {
                return Promise.resolve(false);
            }
            req.getSession().set("CRM", JSON.stringify(result.records[0]));
            return Promise.resolve(CRMtoLeadInfo(result.records[0]));
        });
    });
}

  • The function CRMtoLeadInfo helps us to convert the CRM lead format to our own JSON object.

Updating the lead in Salesforce CRM

We will update the lead information using the method upsert.

function UpdateCRM(leadInfo) {
    try {
        if (!leadInfo.email || leadInfo.email === "") return;
        var data = {};
        if (leadInfo.firstName && leadInfo.firstName !== "") {
            data.FirstName = leadInfo.firstName;
        }
        if (leadInfo.lastName && leadInfo.lastName !== "") {
            data.LastName = leadInfo.lastName;
        }
        if (leadInfo.phoneNumber && leadInfo.phoneNumber !== "") {
            data.Phone = leadInfo.phoneNumber;
        }
        if (Object.keys(data).length === 0) return;

        data.ExtId__c = leadInfo.email;

        var conn = new jsforce.Connection();
        return conn.login(userName, password).then(function(userInfo) {
            return conn.sobject("Contact").upsert([data], data.ExtId__c);
        }).catch(function(err) {
            console.error(err);
        });
    } catch (err) {
        console.error(err);
    }
}

Test your new application

Now that your app can collect and update CRM data, try out what you have so far.

In the chat console on the right, type the request “Please ask sales to contact me”. After you type the request, hit “Send”.

The chatbot will  collect your contact information and will offer to connect you with the sales department

Need help?

Feel free to contact us, we are happy to help and answer any question you may have.

 

Was this article helpful to you? Yes 1 No

How can we help?