Integrate Salesforce and IBM Watson - Tone Analyser Experiment

This experiment propose to detail how to quickly integrate Salesforce and Watson, for that I will take the Tone Analyser Service as an example. This service provided by the Watson platform uses linguistic analysis to detect emotions, social tendencies and language styles in the provided content (Text or Webpage).

In order to give a reason to this integration, I have imagined a simple use case: a Salesforce organisation has implemented the Case Management to receive questions or comments from customers through their public website. They would like to provide an on-demand solution to analyse and get insights from these customer communications.

Experiment assumptions

  • Salesforce development/admin basic knowledge
  • Javascript development basic knowledge
  • Lightning Design System basic knowledge
  • Salesforce developer org available

Watson Account

Owning a Watson account is quick and free, and more than that, it is required to complete this experiment. Simply follow the link below to register and get your personal workspace:

Watson Tone Analyser Service

Once your account is activated, create a new service and select the Tone Analyser:

Tone Analyser Service selection screen (With a little of French in it)

The service creation will automatically generate credentials for it, those can be accessed from the Service management page, it should look like this:

Service credentials screen (Still with French content)

Keep this Tab open or store this JSON somewhere, we will use it later in this experiment. At that point, you have a Watson workspace up and running and the Tone Analyser Service deployed and ready to use. That’s the only actions that will happen on Watson, next steps will take place directly on Salesforce.

Salesforce implementation

On Salesforce, we will implement multiple components:

  • A Named Credentials to store Tone Analyser Service URL and credentials
  • A Apex Class to manage the REST API Callout and JSON deserialisation of the response
  • A Case Custom Quick Action: A VisualForce page and an Apex Controller Extension. The VisualForce will be based on Lightning Design System and will display the returned results of Watson Service

Named Credentials

The first component to configure is a Named Credentials, that will store the callout informations generated during the previous step.

A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To simplify the setup of authenticated callouts, specify a named credential as the callout endpoint.

You should end up with something similar to this:

Named Credentials — Tone Analyser Service

Tone Analyser Service

An Apex Service will help to connect to the Tone Analyser REST Api, passing the content to be analysed and deserialise the response to be consumed or displayed. In our use case, we will pass the Case Description to this Service and the response will be transformed into Apex classes.

Information: The Service also returns an analysis per sentence that is not part of my deserialisation, feel free to implement it on your own starting from this source code.

REST Api Documentation

Case Quick Action

Below is the final result in action !

In order to implement this quick action for the Case object, we will create two main components: the view (VisualForce Page) and the controller extension (Apex Class).

The view will perform the following actions:

  • Display a spinner by default
  • Call the controller extension for retrieving the analysis
  • Process the returned analysis: Hide the spinner and display the analysis results in three columns.

The Controller extension will simply expose one method that takes a Case Id as a parameter and call the Tone Analyser Service.

Once done, the last missing piece will be to transform this VisualForce page into an actual QuickAction that will be displayed in the Case Layout.

First, create the Quick Action under Setup | Case | Buttons, Links and Actions:

Case Quick Action

Then, add the created action in you Case Layout, under “Salesforce1 and LeX Actions”:

Case Layout — LeX Quick Actions

It’s alive, aliiiiive !

Final Words

All the code presented here is available in a Github repository, it can be improved, for sure, especially on the Design/UX aspect, by adding colours or charts for example instead of simple values.
And you, have you already or do you plan to work with Watson and Salesforce ?
Feel free to share with me any comment, feedback or experience.

Happy Experimenting …