JavaScript

Update Express app to make API calls to Salesforce

In this section, you will update your Express application to create a new Task on Salesforce when your webhook is triggered from a call.

To update an ExpressJS application:

  1. In your application, add the JSForce library as well as dotenv by using:

    npm install jsforce dotenv--save
  2. Create a new file called .env, which will have the following:

    The SF_USERNAME and SF_PASSWORD will be the username and password used to login into Salesforce. The SF_TOKEN is the token you should have received via email when your security token was generated by Salesforce.

  3. Create a new Javascript file, called Salesforce.js and add the following:

  4. Update the code in app.js to import this new file:

    When the app loads, write the code to login using your Salesforce credentials.

  5. Update the code in the app.post('/webhook) section of your application to use the new Salesforce.js file.

    This code will be triggered when a call is made or received from your VBC number, When the call is completed(if (state == "ANSWERED")), the application will first look for a Contact with the given phone number(event.phoneNumber).

    This will call the salesforce.getContact() function to search for the Contact. If the Contact exists, we create a new Task using the function salesforce.addTask(). This will create a new Task in Salesforce that includes the title, the associated Contact(using the contactId) and the duration of the call.

    If there are no Contacts that match the given phone number, using the contact["totalSize"] == 0 check, the application will then create a new Contact using the event.callerId property from the webhook, and split the string into a first name and last name. Note, outgoing calls MAY not have this property. In this case, we will use the phone number as the Contact's last name.

  6. To start your application, run the following command:

    node app.js

Your application will now create a new Task in Salesforce when a completed call is made or received.

Note: Make sure the port you have specified (300) is the same port you use when creating your ngrok URL.