If you saw our Einstein Discovery Predictive CSAT video, you probably got the itch to set predictive CSAT scoring up in your own Salesforce org. To make life easier for you, we are providing a step-by-step guide on how to set up a writeback from Einstein Discovery to the Salesforce platform. While this guide will cover the CSAT scoring use case, you should also be able to use it to set up any other use case, whatever the prediction and object may be. By following these seven easy steps, you can have your own predictive CSAT model up and running in no time! Please note that these steps will work as of the Summer ’18 release.
Prerequisites
To set this up you are going to need both the Einstein Discovery Analyst Permission Set License and the Einstein Discovery Manager Permission Set assigned. You are also going to need the Connect Einstein Discovery Model permission assigned; we recommend creating a separate Permission Set for this. We also recommend you be familiar with how to create an Einstein Discovery Story before deploying your first Writeback. Trailhead is a great resource for this – here is a great place to get started.
Step 1 – Install Managed Package
The first step is to install a managed package from Salesforce that enables Einstein Discovery recommendations in a Salesforce object. Navigate to this link. If prompted, enter your login information. Ensure that Install for Admins Only is selected, and click Install. On the next screen, select Yes, grant access to these third-party websites and click Continue.
Step 2 – Create and Deploy an Einstein Discovery Story
The second step is to create and deploy an Einstein Discovery Story. In our Predictive CSAT example, we would set the story up to try to maximize the CSAT value on the Case object. From your Story, click the down arrow next to Share & Export, then Deploy Story. Specify a name for the Prediction. Let’s call ours Predictive CSAT! Follow the prompts by clicking Next and Deploy.
Step 3 – Create Custom Fields in Salesforce to Display Recommendations
Next, create three custom fields in Salesforce that will contain the predictions from Einstein Discovery. These fields will contain the measured outcome, an explanation and prescription generated by the Einstein Discovery Story. In our use case, these fields will be created on the Case object. Note that you can call these fields whatever you want; just make note of the field API name, as you will use this in a future step. Here are the details on the fields you will need to create:
Outcome Field
Name: Predicted CSAT
Type: Number
Field-Level Security: Read-Only
Explanation Field
Name: CSAT Explanation
Type: Text Area (Long)
Field-Level Security: Read-Only
Prescription Field
Name: CSAT Prescription
Type: Text Area (Long)
Field-Level Security: Read-Only
Step 4 – Connect Einstein Discovery to Your Custom Fields
The next step is to connect Einstein Discovery to the three custom fields you created in the previous step. From Setup, search for Custom Settings in the Quick Find box, then click on Custom Settings. Next to Einstein Discovery – Writeback, click Manage, then New. Fill in all the details on this page. After you are done, hit Save. Here are some detail on the fields:
Name: Give the Custom Setting a Name. Make note of it, as we will use this in a later step. Let’s call ours CSATPrediction.
Analysis Id: The Id of the Einstein Discovery Story you want to writeback predictions for. To get the Id, navigate to Einstein Discovery, hit the drop down next to the story, click About Story, then copy the Analysis Id and paste it into the Analysis Id field on the Custom Setting.
Commentary Field Name: The API name of the Explanation field you created in Step 3; in our case, CSAT_Explanation__c.
Outcome Field Name: The API name of the Outcome field you created in Step 3; Predicted_CSAT__c.
Prescription Field Name: The API name of the Prescription field you created in Step 3; CSAT_Prescription__c.
Object: The Salesforce object where you added the custom fields; Case.
Step 5: Create an Apex Trigger
The fifth step is to create an Apex trigger. From Setup, click on the Object Manager tab, then select the object you are creating recommendations on, which is Case for us. Click on Triggers, then New. Paste this code into the text area.
trigger SetDealPrediction on Case (after insert, after update) {
if(System.isFuture()) return;
if(analytics_ed.CheckRecursive.runOnce()) {
// custom Settings’ name
String CONFIG_NAME = ‘CSATPrediction‘;
analytics_ed.TriggerHandler.insertUpdateHandle(CONFIG_NAME);
}
}
Importantly, note the two highlighted texts. Case is the name of the object you are creating the trigger on. CSATPrediction is the name of the Custom Setting in Step 4. Beaware that this code will only work if the Einstein Discovery Story contained only fields from the Case object with no data transformation. The code becomes more complicated if multiple objects are used, or if a data transformation occurs. For details on the those more complicated use cases, see the appendix at the end of this blog post.
Step 6: Add Einstein Discovery Recommendations to Case Page Layout
The last Discovery related step is to add the recommendations to the page layout. Start by navigating to any Case record. Click on the Gear icon in the upper-right hand corner. Drag the Einstein Discovery component onto the page layout. This component will show the Outcome, Explanation and Prescription predictions. It will automatically connect to the custom fields you created. Click Save, and if prompted, Activate. Your Discovery steps are now completed! Any time you create or save a record, your predictions will now appear in the component you added to the page layout. You will now see your predictive CSAT scores on all cases! Your work is not done there, however. To really bring out the power of Salesforce, let’s show how to use Escalation Rules, Workflows and Einstein Analytics in conjunction with your Discovery predictions.
Step 7: Post-Discovery – Escalation Rules, Workflows & Einstein Analytics
Escalation Rules: First, let’s cover how to set up an Escalation Rule. An Escalation Rule will automatically escalate a case if a predefined set of criteria are met. Let’s set up a rule that escalates a case if the predicted CSAT score is less than 75. From Setup, search for Escalation Rules in the Quick Find Box. Give the rule a name, check Active and click New. Next, click New under Rule Entries. Enter 1 in the Sort Order, then for Step 2, enter the criteria when Predicted CSAT is less than or equal to 75. Click Save.
Next, click New under Escalation Actions. Specify the time criteria for the rule; essentially, when the Escalation Rule will be kicked off. Let’s set it at 0 hrs 30 mins. Then decide who to assign it to; in our case, the High-Risk Queue (note that you may have to create this queue, which can be done via the Queues section in Setup). Additionally, select a Notification Template. We recommend creating one that highlights that a high-risk case has been assigned to them (this can be done through the Email Templates section in Setup). Lastly, hit Save.
Workflow Rules: Workflow rules are created similarly to Escalation Rules. Navigate to Workflow Rules from Setup. Set a Rule Criteria, just as in an Escalation Rule. Then set a Workflow Action. Since we’re already sending out an email alert in the Escalation Rule, let’s set up a Task. Assign this to the Case Owner, set a Due Date, and enter a Comment that tells the user what the Task is about.
Einstein Analytics: Einstein Discovery predictions are even stronger when you can export them to Einstein Analytics for further analysis. For our predictive CSAT example, you could create a dashboard that shows open cases with the lowest predicted CSAT scores. Let’s navigate to the Analytics Studio to create that. If you’ve already created a Case dataset, you will only need to add those three new prediction fields you created, which you can do through the Data Manager. If you don’t have a Case dataset, click Create, then Dataset. Select Salesforce as your data source. Give the dataset a name and add it to a dataflow or create a new one. Next, select Case as your object and select all the fields you want in the dataset. Just make sure to include those three prediction fields you created. You can also add object relationships to the dataset, such as Case Owner and Account. Once the dataset is completed, make sure the dataflow runs. You are now ready to start creating your dashboard with the predicted CSAT fields!
Appendix
The code snippet in step 5 to create the Apex trigger only worked if the Einstein Discovery Story contained fields from one object. It would not work if multiple objects were used, or if a data transformation occurred. Here are examples of code for every potential scenario. The items in bold are what you will need to change.
Single Object, no data transformation
trigger SetDealPrediction on [ObjectName] (after insert, after update) {
if(System.isFuture()) return;
if(analytics_ed.CheckRecursive.runOnce()) {
// custom Settings’ name
String CONFIG_NAME = ‘[NameOfCustomSetting]’;
analytics_ed.TriggerHandler.insertUpdateHandle(CONFIG_NAME);
}
}
Multiple Objects, no data transformation
trigger SetDealPredictionByAccount on Account (after insert, after update) {
if(System.isFuture()) return;
if(analytics_ed.CheckRecursive.runOnce()) {
// custom Settings’ name
String CONFIG_NAME = ‘Deal’;
// custom query
String CUSTOM_QUERY = ‘[SOQLQuery]’;
analytics_ed.TriggerHandler.insertUpdateHandleForCustomQuery(CONFIG_NAME,CUSTOM_QUERY);
}
}
Multiple Objects, with data transformation
trigger SetDealPredictionWithDerivedFields on Deal__c (after insert, after update) {
if(System.isFuture()) return;
if(analytics_ed.CheckRecursive.runOnce()) {
// customSettings’ name
String CONFIG_NAME = ‘Deal’;
// convert the list of objects into a list of field-value map
List<Map<String, String>> fieldValues = (List<Map<String, String>>) JSON.deserializeUntyped(JSON.serialize(Trigger.new));
for (Map<String, String> fieldValueMap : fieldValues) {
// add derived fields
String productName = fieldValueMap.get(‘Product__c’);
if (productName.equals(‘P100’)) {
fieldValueMap.put(‘Product_Option__c’, ‘O100’);
fieldValueMap.put(‘Product_Warranty__c’,’W100′)
} fieldValues.add(fieldValueMap);
}
// make setPrediction call
analytics_ed.TriggerHandler.insertUpdateHandleForFieldValues(CONFIG_NAME,JSON.serialize(fieldValues));
}
}