Automatic Email Classification Using Salesforce Einstein

Do you own a company that receives a lot of emails? Have you assigned employees for segregating emails into categories and then transferring these emails to the concerned section in your company? If so, then here’s a solution to your problem, which saves not only your investment in terms of money but in terms of time as well.

With the help of Salesforce Einstein you can create a system that automatically categorises all your emails and forwards it to the concerned section. You just need to provide the model with the data mapped to labels, that is, provide the emails that your company has received in the past along with their mapping to departments. This data can then be used to “train” Einstein. Once this model is trained on the data provided by you, you are good to go!

Here’s a brief description of the procedure that needs to be carried out in order to achieve the desired output:
  • Prepare the dataset (Training Dataset).

    Training Dataset

  • Upload the prepared dataset online.
    Once you have prepared the dataset, upload the excel sheet on Google drive or any other similar online platform and get the link for the same.

  • Creating a Salesforce Einstein Account
    In order to train a model, you first need to access Einstein API. Following steps will help you in achieving this goal.

    • Navigate to https://api.einstein.ai/signup.
    • Put in your credentials and download the key to your local system.
    • Install Curl or Postman for sending requests and receiving response from APIs.
    • Generate token by visiting https://api.einstein.ai/token, Enter your email address and upload the key that you downloaded in the previous step. Click generate token and copy this token somewhere so you can access it later.

  • Uploading the dataset (Training Dataset) to Einstein
    Enter the following curl command to upload your dataset.
    curl -X POST -H "Authorization: Bearer <Token>" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "type=text-intent" -F "path=<Path of File uploaded on Google Drive>" https://api.einstein.ai/v2/language/datasets/upload

    You get the dataset ID in response.

  • Check dataset if it’s uploaded or not.
    In order to check if the dataset is properly uploaded or use the following curl command.

    curl -X GET -H "Authorization: Bearer <Token>" -H "Cache-Control: no-cache" https://api.einstein.ai/v2/language/datasets/<DatasetID>

  • Training the Model
    Enter the following command to train the model on the dataset provided as input.
    curl -X POST -H "Authorization: Bearer " -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "name=Case Classification Model" -F "datasetId=<DatasetID>" https://api.einstein.ai/v2/language/train

    The response of this request return the Model ID of the model being trained.

  • Get the Training Status
    Enter the following command to get the status of training process curl -X GET -H "Authorization: Bearer <Token>" -H "Cache-Control: no-cache"https://api.einstein.ai/v2/language/train/<;ModelID>

Once the above mentioned steps are all carried out, your model is ready to be used for prediction of any new Email and assign it an appropriate label on the basis of its content. The following is the process used for finding label for any new Email received.

  • Predicting Label for Mail
    curl -X POST -H "Authorization: Bearer <Token>" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "modelId=<ModelID>" -F "document=<Email Content>" https://api.einstein.ai/v2/language/intent
    Response to above request is something similar to the below mentioned response: {"probabilities":[{"label":"SalesforceSupport","probability":0.99716944},{"label":"Others","probability":0.0013461004},{"label":"Training","probability":8.565797E-4},{"label":"JobApplication","probability":5.614456E-4},{"label":"Klipfolio","probability":3.7095313E-5}],"object":"predictresponse"}

The above response gives the probabilities of each label for the input Email text.
Once the label for email is predicted, it can be forwarded to the concerned person by using Apex code.