Sunday 22 July 2018

Einstein Bots with Apex Class | How to Call Apex Class From Einstein Bots


In the last post we discussed how to setup Einstein Bots. In this post we will call apex class from Einstein Bots.

Step 1) Setup Live Agent . You can check this post to setup Live Agent.
Step 2) Enable Einstein Bots. Take a help from this post.
Step 3) Create one Apex Class with "@InvocableMethod" Method.


public with Sharing Class ChatBotTemperature
{
    public class ChatBotTempOutput{
        @InvocableVariable(required= true)
        public String City ;

        @InvocableVariable(required= true)
        public Integer Temp ;
    }

    public class ChatBotTempInput{
        @InvocableVariable(required= true)
        public String City ;
    }

    @InvocableMethod(Label ='Get Temperature' description='return temp')
    public static List<ChatBotTempOutput> getTemperature(List<ChatBotTempInput> input)
    {
         List<ChatBotTempOutput> lstOutput = new List<ChatBotTempOutput>();
         for( ChatBotTempInput ChatBI : input){
             ChatBotTempOutput obj = new ChatBotTempOutput();
             obj.City = ChatBI.City;
             obj.Temp = 10; // You can make some callout to get real Tem
             lstOutput.add(obj);
         }
         return  lstOutput;     
    }
}


Step 4) Add The Apex class in  "sfdc.chatbot.service.permset" permission set. This is the Important part.


Step 5) Setup Einstein Bot

Create new bot
 Create Dialog

Add Question
Add New Slot to get City Value
Add Action. 
 All Input and Output Invocable Variable will come after selecting Action (Invocable Method).
Show Response.

Step 6) Here is output 


How to setup Bot. Please check below recording


Please share your feedback and comment.

Thanks
Amit Chaudhary





6 comments:

  1. Oliver Westlake-Simm5 February 2019 at 21:53

    Hi Amit,

    I created an Invocable Apex Class for the Einstein Bot to Search Articles within the community. However, I'm encountering an issue with the test class. It looks like the test article that's being inserted doesn't get returned in any of the search results for the bot so i'm stuck at 68% code coverage and have been for a week. Have you heard of this?

    ReplyDelete
  2. Where is the test class? Could you please add that as well?

    ReplyDelete
  3. Hi Amit,

    I am working on Existing Einstein Bot in my project which is calling Apex class and is working perfectly. But when I try to add new input Variable and try to chat it shows Message as "No Agents Available". Any idea why this is showing up?

    I just modified the Apex class and added new Variable on both Apex class and Einstein Bot

    ReplyDelete
  4. You can check the debug of process in Performance->Event logs with each steps and can trace where the error has been occurred

    ReplyDelete
  5. very useful contain✔👍👍

    ReplyDelete