August 11, 2019

How to call Apex from Einstein Chat Bot?

Sample Apex Class:

  1. public class EinsteinChatBotController {  
  2.   
  3.     public class CaseOutput {  
  4.       
  5.         @InvocableVariable( required=true )  
  6.         public String sStatus;  
  7.     }  
  8.       
  9.     public class CaseInput {  
  10.       
  11.         @InvocableVariable( required=true )  
  12.         public String sCaseNumber;  
  13.           
  14.     }  
  15.   
  16.     @InvocableMethod(label='Get Case Status')  
  17.     public static List < CaseOutput > fetchCaseStatus( List < CaseInput > listCaseInputs ) {  
  18.       
  19.         List < CaseOutput > objOutputs = new List < CaseOutput >();  
  20.         CaseOutput objOutput = new CaseOutput();  
  21.         Set < String > strCaseNumbers = new Set < String >();  
  22.         for ( CaseInput objCaseInput : listCaseInputs )  
  23.             strCaseNumbers.add( objCaseInput.sCaseNumber );  
  24.         objOutput.sStatus = [ SELECT Status FROM Case WHERE CaseNumber IN: strCaseNumbers LIMIT 1].Status;  
  25.         objOutputs.add( objOutput );   
  26.         return objOutputs;  
  27.       
  28.     }  
  29.   
  30. }  

Apex Class action setup in Einstein Bot:


Permission Set sfdc.chatbot.service.permset:
Make sure the Apex Class is added to the Permission Set sfdc.chatbot.service.permset.


2 comments:

  1. Hi ! Can we show 2 results in Bot window. ??
    I have scenario in which Bot should return account number of the account, which is working fine for account, issues is when 2 Account of same name exists then Bot should display 2 account number instead of only one account number.

    ReplyDelete
    Replies
    1. Check this - https://www.infallibletechie.com/2020/06/einstein-bot-getting-multiple-values.html

      Delete