Hello World Code for Salesforce Einstein

As a programmer, We do code in many languages. We create always a program named as Hello World.

This blog is all about for Hello World program for Salesforce Einstein. In this blog, I am not going to tell you how does Salesforce Einstein work. This is just about how to get started and write your first program in Salesforce Einstein. I will cover both Salesforce Einstein Vision and Salesforce Einstein Language.


A. Get the key:

Follow Steps from here

And read until you get the einstein_platform.pem (predictive_services.pem) file.

Note: Keep this file very carefully. Don’t loose it.

B. Upload the key file to Salesforce:

Now upload above .pem file into Salesforce org. Go to Files tab and upload this file here.(https://ap5.salesforce.com/_ui/core/chatter/files/FileTabPage – URL from my org for the File tab)

C. Remote Site Setting:

We need to add the remote site setting. See here: https://metamind.readme.io/v1/docs/apex-qs-create-remote-site

Note: As we will be using https://api.einstein.ai in EinsteinMaster class. We need to add https://api.einstein.ai in remote site instead of https://api.metamind.io

D. Copy Code:

Git Repo is here. (Salesforce-Einstein-Custom-Code https://github.com/vishnuvaishnav/Salesforce-Einstein-Custom-Code )

1. EinsteinMaster.apex: Download or Copy EinsteinMaster.apex. You don’t need to touch this file much. Just need to put the email id of yours. ( In the variable name  as USER_EMAIL). Then save it.

Note: This apex code is a combined and modified code of multiple Salesforce code files. I did some modification for the developers. So they don’t need to worry about multiple files. 

2. EinsteinExampleCtrl: Download or CopyEinsteinExampleCtrl.apex. This is example code for prediction functions. I am using 5 functions here.

  • Vision API Example – Prediction From URL: It will do prediction from given image URL.
  • Vision API Example – Prediction From Blob: It will do prediction from given Blob data ( I am using attachment’s body).
  • Vision API Example – Prediction From Base64:  It will do prediction from given Base64 data. You can generate base64.

Note: For the above 3 image vision prediction. I am using GeneralImageClassifier model. You can use your own model here. Just need to pass the Model ID in the function.

  • Language API Example – Sentiment: It will return the sentiment of given text.

Note: For the above Sentiment prediction. I am using CommunitySentiment model. You can use your own model here. Just need to pass the Model ID in the function.

  • Language API Example – Intent: It will return the intent of given text.For intent, we don’t have any predefined model. So we need to create a model for this.Read here https://metamind.readme.io/docs/intent-quick-start-custom-classifier#section-step-1-define-your-classes-and-gather-data to create an Intent model. After that pass the Model ID here.

3. EinsteinExample (VF Page): Copy this from here https://github.com/vishnuvaishnav/Salesforce-Einstein-Custom-Code/blob/master/EinsteinExample.page . It contains example of Vision API Example – Prediction From URL and Language API Example – Sentiment.

If you want to see or get the access token. You can use EinsteinMaster.getAccessToken(); in the code.

All done? Now run the page and do changes in EinsteinExampleCtrl and EinsteinExample page as per your requirement.