Miracles can happen with Watson and Salesforce

After a long time I am writing this blog to share my experience with some ML stuff. Over past few days I made myself busy in learning about IBM Watson apis and how can they help Salesforce customers in their businesses.

Thanks to Salesforce trailhead module  getting ramped up on IBM watson SDK for Salesforce was a piece of cake. During the trail one can learn about how to setup federated search in Salesforce in order to search through the files in IBM Watson.

Below is a screenshot of my IBM Watson collection. Note all the ML stats that comes readily baked from IBM Watson

I have 34 documents uploaded in my collection as soon as the documents are uploaded IBM Watson massages the data and finds out top topics getting discussed in the document, overall sentiments in those documents, finds out the context(concepts) that the document is talking about, classification of those documents upto 5 levels etc.

All the mentioned above aspects are already explained in multiple blog sources. What I am interested in sharing with you all is the use case scenarios where we can leverage this feature.

Salesforce has 3 primary standard Objects - Accounts, Opportunities and Cases. Most of the time there is a need of getting relevant informations. This implies a strong need of recommendation services. Among the recommendation features, files recommendation is an important aspect. Netflix leverages this aspect a lot. Based on context of what you are watching it tries to recommend you the next probable movie. 

Similarly in Salesforce, based on the opportunity and the stage that you are in, recommendation feature can help the customer with the relevant file that is going to help him/her close that opportunity or deal.

Earlier it was difficult because Salesforce still don't provide Smart search feature for files. But with IBM watson NLP search feature we can create an intelligent component for the same.

Use Case Scenario: As a customer I should get document recommendation for closing my deal/opportunity, case or account

Acceptance Criteria:
  1. If I change the opportunity stage from prospect to negotiation/review the component should be able to recommend me all the relevant documents which are similar to this opportunity and were used in such similar stage.
  2. If a support person searches for something like "Show me relevant files to fix login issues" the component should respond back with all the relevant examples
There are such n number of examples like these which can be solved with such component.

Such scenarios can be achieved using the IBM Watson Discovery api for Natural Language Processing. Let me show you the API call and then discuss about that briefly.

Coding Time = (2hrs Trailhead + 3 hour Apex code + 1 hour testing)

It took me around 5-6 hours to get this thought into reality. 

Step 1: We need to create an apex controller which will be invoking the IBM Watson Discovery API through Heroku Service. 

    public static String getSearchResults(String query) {
        IBMDiscoveryV1 discovery = new IBMDiscoveryV1('2017-11-07');
        IBMDiscoveryV1Models.QueryOptions options
                = new IBMDiscoveryV1Models.QueryOptionsBuilder('<<environmentId>>',
                        .naturalLanguageQuery (query)
                        .returnField(new List<String> {'url','extracted_metadata','enriched_text'})
        IBMDiscoveryV1Models.QueryResponse response = discovery.query(options);
        return response.toString();

With this small code snippet we can create our own intelligent component. In here we are query a NLP string which goes as a NLPQuery. In response we ask Watson to give us extracted metadata like file name, title etc. along with enriched text like Top Topics getting discussed, Context of those documents, Classification categories etc.

Step 2: Below is the javascript sample where we can create an NLP query such as "Give recommended files for <opportunity name>> in stage <<Opportunity Stage>> Such query can help us mine through our vast data and retrieve other similar opportunity documents where files were uploaded or created by other teams.

getQuery : function(cmp, id) {
        var self=this;
        var action = cmp.get('c.getSearchResults');
        var acctAction = cmp.get('c.getOpportunity');
        acctAction.setParams({recordId: id});
        acctAction.setCallback(this, function(response) {

            var result = response.getReturnValue();
var query = "Get recommended files for " + result.Name+" in stage "+ result.StageName;
        action.setParams({name: query});
            action.setCallback(this, function(response) {
                var state = response.getState();
                let fileNames = new Set();
                if(cmp.isValid() && state === 'SUCCESS') {
                    var resp = response.getReturnValue();
                    var respArr = JSON.parse(resp)["results"];
                        var fileName = file['additional_properties']["extracted_metadata"]["filename"]
                        fileName=fileName.replace(/\.[^/.]+$/, "");
                    self.getUrl(cmp, Array.from(fileNames));

Note there are 2 nested actions. First action will get the opportunity Id and do a SOQL query to get the opportunity name and stage

    public static Opportunity getOpportunity(Id recordId) {
        Opportunity oppt = [SELECT Name, StageName FROM Opportunity WHERE Id=:recordId LIMIT 1];
        return oppt;

Second action creates the NLP query with the results of the first action and send it to IBM Watson to get the response. In the javascript controller I am just getting the filename from the extracted metadata, this can be enhanced to get top topics for that document, sentiment analysis and classification categories. 


In this blog I wanted to share my experience of creating a lightning component within hours which is equipped with AI features in order to break the apprehension that comes along with AI requirements. I am trying to enhance this component with more Watson responses and hope to share those experiences with you all pretty soon. As always thanks for inspiring me by giving your feedback on my blogs. If you need more details regarding this, do let me know in the comment section. 


  1. I’ve been your silent reader for quite some time and now I’m delighted to say that I’m inspired by your articles. You have shared very valuable information and knowledge that people should recognise. Thank you for sharing. I would love to see more updates from you.

    social media marketing


Post a Comment

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module