? salesforce integration with github

Shen Yifeng

salesforce enthusiast,sfdc developer, consultant

Using Google Prediction API with Salesforce

Integration with Google API

This is a sample program picked up from this book Learning Salesforce Einstein which typically demonstrate how to integrate the third-party library on the force.com platform. And this blogpost is acknowledged by the book of the author. Acknowledgement

To metion: Google Predition API is now deprecated and the service of this api is stopped, all the pictures are taken in the past when it was still online. We can use other Google ML API instead of this.

The scenario of this case is to predict the probability of the Salesforce opportunity closure based on the expected revenue and the opportunity type. The following is the training data.

Here is the distribution on my local machine using Anaconda.

How to implement?

Here are the requirements:

  • Load the sample dataset; Train the data; Use the generated function to predict the outcome for the new dataset.
  • Set the authentication by using OAuth for calling API from SFDC. On the SFDC side, set the Auth. Providers with Consumer Key and Consumer Secret which generated from the oauth consent from the Google API side. Use Named Credentials so as to avoid passing the authorization token.
  • Make an asynchronous API call to invoke the Google Prediction API

Training data:

Training response:

Create a web application which will generate the Consumer Secret and Consumer Key.

This is the needed configuration in SFDC side:

The Apex class that is invoked from the trigger is as follow:

//Apex Class to make a Callout to Google Prediction API
      public with sharing class opportunityTriggerHelper{
        public static void predictProbability(Id OpportunityId){
          Opportunity oppData = [Select Id,Amount,Type,
          Predicted_Probability__c from Opportunity where Id =
          HttpRequest req = new HttpRequest();
          //Form the Body
          PredictionAPIInput apiInput = new PredictionAPIInput();
          PredictionAPIInput.csvData csvData = 
          new PredictionAPIInput.csvData();
          csvData.csvInstance = new list{
          apiInput.input = csvData;
          Http http = new Http();
          HTTPResponse res = http.send(req);
          if(res.getStatusCode() == 200){
            Map result = (Map)
            oppData.Predicted_Probability__c = 
            update oppData;

The result:

Enjoy! You can see the full code in the github/PacktPublishing/Learning-Salesforce-Einstein

Machine Learning Algorithms

I think it would be better to understand more ML knowledge besides this Linear Regression example. There are so many complicated algorithms such as : Naïve Bayes Classifier Algorithm, K Means Clustering Algorithm, Support Vector Machine Algorithm, Apriori Algorithm, Logistic Regression, Artificial Neural Networks, Random Forests, Decision Trees, Nearest Neighbours......

More samples:

Integration through the JWT bearer token flow.

About the author of the book: Mohith Shrivastava

This blogpost is originated from a impressive and well explained book which is written by the author - Mohith Shrivastava who is an awesome and inspiring man