Create and Search Salesforce Objects from Einstein Bot.

Posted on August 11, 2021 Post Thumbnail

Hi Trailblazers, In this blog post We are going to implement object creation and search from Salesforce in Einstein Bots.

Let's have a look at the scenario itself.

We need to create case from Einstein Bot and search status of case using case number directly from Einstein Bot.

Before Starting, Let's have a look at demo.

Check out demo :)

For a quick demo,  All you need to do.

Click on the right bottom button "Chat with an Expert"

After Greeting Messages, answer quick questions and click on Case.

Check following image for reference.

After Greeting Messages, answer quick questions and click on Case.

After Greeting Messages, answer quick questions and type Case.

After Greeting Messages, answer quick questions and type Case.

Click on Create a Case provide details.

Enter details

Provide these details.

Case created

Case created in Salesforce org and Case Number showing here.

Now, with this case number, you can track your case status.

See following image for reference.

See case details

That's all about demo.

Start :)

We need to create Case Dialog which contains following dialogs

1. Create a Case

2 Case Status 

For creating Create a Case dialog,

First we need to create an apex class with some invocable variables and invocable method.

Invocable Variables:

As you know, We can pass only one list parameter in Invocable method, If we require multiple parameter to pass, We use invocable variables. It's just a wrapper class with @InvocableVariable

Syntax for invocable variables.

 public class CreateCaseInput{
        @InvocableVariable(required = true)
        public String Subject;
        
        @InvocableVariable(required = true)
        public String Description;
        
        @InvocableVariable(required = true)
        public String Email;
        
    }

Create a class: CreateCaseFromBot

public with Sharing Class CreateCaseFromBot{
    public class CreateCaseInput{
        @InvocableVariable(required = true)
        public String Subject;
        
        @InvocableVariable(required = true)
        public String Description;
        
        @InvocableVariable(required = true)
        public String Email;
        
    }
    @InvocableMethod(Label = 'Create a Case' Description ='Method for creating a case')
    public static List<String> getCase(List<CreateCaseInput> input)
    {   
        Case csObj = new Case();
        csObj.Subject = input[0].Subject;
        csObj.Description =  input[0].Description;
        csObj.Status = 'New';
        csObj.Origin = 'Web';
        csObj.SuppliedEmail = input[0].Email;
        
        insert csObj;
        List<String> newCaseList = new List<String>();
        String caseNum = [SELECT CaseNumber FROM Case WHERE Id =: csObj.Id].CaseNumber;
        newCaseList.add('Your Case Number is: '+caseNum);
        return newCaseList;   
    }
}

Test class: CreateCaseFromBotTest

@isTest
public class CreateCaseFromBotTest {
    static testMethod void test1() {
        List<CreateCaseFromBot.CreateCaseInput> cList = new List<CreateCaseFromBot.CreateCaseInput>();
        CreateCaseFromBot.CreateCaseInput c = new CreateCaseFromBot.CreateCaseInput();
        c.Subject = 'heysftest';
        c.Email = '[email protected]';
        c.Description = 'description for test case';
        cList.add(c);
        test.startTest();
        List<String> returnedList =  CreateCaseFromBot.getCase(cList);
        test.stopTest();
        String casenumber = 'Your Case Number is: '+[SELECT CaseNumber FROM Case LIMIT 1].CaseNumber;
        system.assertEquals(casenumber, returnedList[0]);
    }
}

After creating class, go to following permission set

sfdc.chatbot.service.permset 

And give access to CreateCaseFromBot class and Case Object.

From Object Settings and Apex Class Access

See it.

We are done with it, Now we need to create 3 Variables to Store following details for creating case.

1. Email

2. Subject

3. Description

For this, Navigate to Einstein Bot and Open your Bot.

If you don't know about Einstein Bot, Check other blog posts by searching Einstein.

Search

After creating above 3 variables, Ask questions from Bot and save in Variables.

And Add action: Apex

See

See above picture, Pass variables to apex class and save output in a variable.

To show output, check following image.

Show output.

For creating Case Status dialog.

We'll use standard Search Object Functionality.

First Bot asks for case number:

Asks

Then we choose Search Object from Action.

Check following image for reference.

Selecting ObjectSaving

In above screenshots, we selected object case and fields.

After that saving result in test1(object list).

It returns a list for object.

To access values, We need something like this.

 test1[0].fieldName
Showing all details.

This is how we're showing details of case.

After creating Create a Case and Case Status Dialogs.

We add these dialogs under main case dialog.

see

After performing all above steps, We are able to create Case and search case status in Einstein Bot.

Give it a try and let me know if you have any questions.

If you have any question Ask Me

Thanks for Reading :)

Write a comment for suggestions and hit the heart icon.


1294
1

Tags: #chatBot #EinsteinBot

Comments

  • Puja Avatar
    Puja - 5 months ago
    This is awesome Asif... Keep sharing

Your message is required.

Get notified of new posts