/ SKIL

Integrating SKIL into Salesforce to Predict Deal Success [1/2]

What is Salesforce?

Salesforce is a Customer Relationship Management (CRM) platform, providing cloud-based applications for sales, service, marketing, and more that is delivered as SaaS. It is an important tool for getting connected to your customers. (Visit the Salesforce site for more information.)

Purpose of this Post

In this blog post, we will show you how to integrate SKIL as an intelligence layer within Salesforce.

We'll be estimating the probability of closing a deal for a Salesforce opportunity; that is, we'll develop a neural network using SKIL that will accurately model the probability of closing a deal, given the opportunity details.

Furthermore, we'll write the necessary data transform process to vectorize the Salesforce opportunity data to convert it into a format that our model can understand. We will use the opportunity dataset available in Salesforce for this purpose. Finally, we'll deploy our transform process and model so that Salesforce components can consume it through the SKIL endpoints.

Let's Get Started

We need to do some setup to make SKIL work with the Salesforce ecosystem. Let's begin.

Creating a Salesforce Developer account

First, we need a Salesforce developer account to build our application and integrate it with SKIL. You can create your Salesforce developer account here.

Creating-salesforce-developer-account

Enter your details and credentials to create the account. Your username will be an email address (but it doesn't have to be a real one).

Account settings

After creating and logging into your new account, you will need to turn it into an account with its own domain (a necessary step to build and deploy your applications). You can do that by clicking the "settings" icon at the top right of the page and then click "setup" as shown in the image below.
Settings-icon-1

In the top search bar, search for "My Domains". Select that and it will take you to the domains settings page. Check the availability of a given domain, and if it's available, you'll be able to create it.
Searching-for-my-Domain

Note

It takes about two hours for a domain to get set up within Salesforce. You'll receive a confirmation email when it's ready to be logged into.

Now, log into the the domain by clicking the login button next to your domain URL.

Deploying the domain

We're also going to create our custom UI components inside Salesforce. To view them, first you need to deploy your domain and make it available to the outside world. And to do that, you need to go to the settings page and search for "My Domain". Login to the domain you created and click "Deploy for Users". Now your domain is deployed.

Next, you need to add the your SKIL instance URL to the "Remote Site Settings" in Salesforce to allow the outgoing requests from your custom-made Salesforce UI component.

Settings-icon-2
Adding-remote-site-settings
New-remote-site-settings

Viewing Opportunities and Page Editing

Opportunities are defined as "deals in progress."

Here, we are going to use SKIL to identify the probability of closing an opportunity, and show the estimated probability inside the Salesforce application GUI.

Click on the "Application launcher" on the left side of your dashboard and select the "Sales" application.

Application-launcher-and-selecting-the-sales-app

Now you can go to the "Opportunities" tab and create a listview by clicking "new" from the listview controls.

Selecting-opportunities-and-creating-a-listview

Name your listview and wait for it to load, then filter for "Express Logistics SLA". Click "Done" and press "Save" to add the filter.
Filtering-opportunity

See the changes after the filter is applied.

Selecting-opportunity

Now select the filtered opportunity and navigate to the details tab, and it will show you the opportunity details.
Viewing-the-opportunity-details

Now we'll create and add our custom Salesforce component into the details page and connect it to the SKIL instance's deployed transform and model.

Creating a Custom Salesforce Lightning Component

For creating a custom Lightning component, you need to write the UI in an Aura script in XML, a Javascript controller and a backend class in Apex for executing the logic. The code used in this blog is available here.

Salesforce provides a developer console for managing all the code components mentioned above. Click the settings icon and go to the developer console. It will launch into a new brower window.

selecting-the-developer-s-console

A. Writing an APEX Class

For executing the UI backend logic, we need to write a class in Apex which is going to be responsible for logging into the SKIL instance and making requests on behalf of Salesforce.

To create an Apex class go to File > New > Apex Class.
Creating-an-apex-class

Write the name of the file as SKILRequester. Populate it with the code present here.

Make sure to change the HOST_URL variable to your SKIL host. Now, save the file to preserve the changes.

Changing-skil-host-address

We add the @AuraEnabled annotation to make a function callable from Aura code. We'll call the predict method to get the probability prediction.

@AuraEnabled
public static String predict(Id recordId) {
        Opportunity opp = [ SELECT Amount, Probability, ExpectedRevenue, Type,   LeadSource, FiscalQuarter FROM Opportunity WHERE Id = :recordId ];
    String auth = login();
    String ndarray = transform(
        auth,
        opp.Amount,
        opp.Probability,
        opp.ExpectedRevenue, 
        opp.Type, 
        opp.LeadSource,
    	opp.FiscalQuarter);
    
    String pred = predict(auth, ndarray);
    
    return pred;
}

In the above code, we're receiving the opportunity record ID from the Aura component and predicting the probability using the predict method that takes the transformed array (vectorized).

The function first receives the authorization token using the SKIL REST API and it selects the opportunity data we need using the record and then later it predicts and returns the predicted probability.

The transform functions uses the deployed ETL and the predict function used the deployed model. We'll discuss these two in the second part of this blog.

B. Writing an Aura script

For the UI, you need to create a Lightning Component by navigating to File > New > Lightning Component.

Creating-a-lightning-component

Write the name as SmartProb. Copy the content from here into it and save it.

The code is written in XML:

<aura:component controller="SKILRequester" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="opportunity" type="Opportunity" access="global" />
    <aura:attribute name="smartProb" type="String" access="global" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <force:recordData aura:id="service"
                      recordId="{!v.recordId}"
                      targetFields="{!v.opp}"
                      layoutType="FULL" />
    
    <lightning:card title="{! 'SmartProb Score: ' + v.smartProb}">
    </lightning:card>
</aura:component>

The controller specifies the class SKILRequester that we wrote in Apex to program the backend-component logic.

The aura:attribute tag specifies that the component needs the record ID and that it has access to the opportunity. Also, it creates a smartProb field that we'll fill later on. The aura:handler tag specifies the function that the component is going to call (doInit) when it initializes. We'll be writing the logic for it in the Javascript controller in a bit.

The force:recordData tag tells the component to fetch the opportunity and sets it to the v.opp field.

The lightning:card tag creates the component UI. Here we'll show the probability that we're going to fetch through the SKIL endpoints. We'll set this to the field v.smartProb. We just created a simple card component and set the title to the data we want to show.

C. Writing a Controller

To create a Javascript controller, click the create button to the right side of the developer console, next to the CONTROLLER title.
Creating-a-javascript-controller

Copy and paste the content from here and save the file.

In this Javascript controller, we've implemented the doInit method that we called from the Aura component.

doInit: function(component, event, helper) {
    var action = component.get("c.predict");
    action.setParams({recordId: component.get("v.recordId")});
    action.setCallback(this, function(response) {
        var prob = response.getReturnValue();
        component.set("v.smartProb", prob);
    });
    
    $A.enqueueAction(action);
}

In this method, we're calling the predict method from our Apex class and we're passing the recordId that we obtained from the Aura component, and after receiving the probability, we're setting the v.smartProb field with it.

Now, your lightning component is ready to be added in the opportunity details UI. Go back to your sales application page and click the settings icon and select Edit page.

Selecting-edit-page

Scroll down to the custom components and drag and drop the SmartProb component into the details UI.

Dragging-and-dropping-the-SmartProb-component

Save the page and go back to view it. Refresh the page if it's not showing the updates made.

Data Export

We'll use the Salesforce Data Export utility to export the data that we need to train our model. To export the data, go to the setup page and search for Data Export.

Settings-icon-3
Searching-for-data-export

Now click the data export button and export the opportunities. You'll be notified through e-mail when the exported data is available for download.

Exporting-data
Exporting-opportunities

After the data is available, download it from the same Data Export page. Extract the ZIP file to view the CSV in Excel or Google sheets.

Download-exported-data
exported-data-csv

Alternatively, you can download the CSV file from this link. We'll use this same link in our SKIL's Zeppelin notebook to later download the data. The image below shows how that'll look in SKIL.

SKIL-showing-the-details-of-the-exported-data

In the next part of this blog, we'll write the model configurations and the transform process using SKIL and then connect them to Salesforce.