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:
In your application, add the JSForce library as well as dotenv by using:
npm install jsforce dotenv--saveCreate a new file called
.env, which will have the following:The
SF_USERNAMEandSF_PASSWORDwill be the username and password used to login into Salesforce. TheSF_TOKENis the token you should have received via email when your security token was generated by Salesforce.Create a new Javascript file, called
Salesforce.jsand add the following:Update the code in
app.jsto import this new file:When the app loads, write the code to login using your Salesforce credentials.
Update the code in the
app.post('/webhook) section of your application to use the newSalesforce.jsfile.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 functionsalesforce.addTask(). This will create a new Task in Salesforce that includes the title, the associated Contact(using thecontactId) and the duration of the call.If there are no Contacts that match the given phone number, using the
contact["totalSize"] == 0check, the application will then create a new Contact using theevent.callerIdproperty 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.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.
Log incoming calls to Salesforce
Logging incoming calls into Salesforce