Trigger a Batch Job from Einstein Analytics Bulk Action

I am happy to write my first blog on a topic that I enjoy the most. The Bulk action feature of Einstein Analytics has been always a fascinating one for me. This gives the ability to combine data from multiple sources, analyse the data and trigger action based on the analysis. Data Analysis is a growing need for every Business vertical, more so is Insight to Action. Einstein Analytics is one of the best platforms to implement an Action framework for Analytics insights. This article outlines one such action framework where the Einstein Analytics Bulk Action feature can be used to add or modify multiple records in Salesforce based on an Analytics Insight.   In many Businesses, there are scenarios where there is a requirement to add or update data in bulk in Salesforce as an outcome of an insight derived from Einstein Analytics. For example, updating lead records in bulk based on an insight from Einstein Analytics as a lead qualification process. The Einstein Analytics bulk action feature can be used for these requirements.

When there are many thousand records to be added or updated, it is a best practice to use   Batch Job to complete the action in multiple batches.

Creating Batch Job from Einstein Analytics Bulk Action:

The Bulk Action from Einstein Analytics can trigger a Batch job in Salesforce. Here is how:

Step 1:

Create an Apex class to capture the Einstein Analytics SAQL query result with ConnectApi.Wave.executeQuery

query = ApexPages.currentPage().getParameters().get(‘query’);

   ConnectApi.LiteralJson result = ConnectApi.Wave.executeQuery(query);

   response = result.json;

Step 2:

Add the target Salesforce record ids to a list, for example: If the scenario is to bulk update the leads in Salesforce based on the insight from Einstein Analytics, add the lead ids returned from Einstein Analytics to a list variable.

      Set<Id> leadIds = new Set<Id>();

       for(records rec : res.results.records){

    

        leadIds.add(rec.leadId);

       }

Step 3:

Create a Batch Apex to update Lead records

global class BatchLeadInsightsUpdate implements Database.Batchable <SObject> {

//Start Method

global BatchLeadInsightsUpdate( set<Id> LeadRecId){

              this.LeadRecId = LeadRecId;

    }

global Database.QueryLocator start(Database.BatchableContext BC) {

        return Database.getQueryLocator(‘Select Id From Lead Where Id In :LeadRecId’);

    }

    global void execute(Database.BatchableContext BC, List<Contact> scope) {

       << code to update Lead Records>>

    }

//Finish Method

global void finish(Database.BatchableContext bc) { 

   << Code to send email notificatin on batch completion>>

}

}

Step 4:

Pass the Lead Record Ids to the batch apex

public PageReference ExecuteBatch() {

  Database.executeBatch(new BatchLeadInsightsUpdate (leadIds),200);

}

Step 5:

Create a VF Page for Bulk Action

<apex:page id=”EApage” controller=”EABulkActionController”> 

<apex:form>

    <apex:pageBlock>

     <!—Command Button to Invoke Apex Batch — > 

    <apex:commandButton value=”Execute Batch” action=”{!ExecuteBatch}”/>

    </apex:pageBlock>

</apex:form>

</apex:page>

Step 6:

Add the Custom Action to a table widget in Einstein Analytics Dashboard

 

by Gayathri Shivakumar
SOLUTION ARCHITECT