+ Start a Discussion
Michael MMichael M 

Invocable apex not working from einstein bot action

Hello, I am trying to invoke an apex class to get the current user's URL, from my chatbot dialogue. Here is my apex class. It does not seem to work- when it gets to that point in the chatbot, the bot sort of shuts down and says no agent is available. Am I doing this correctly?
public with sharing class BotUrlClass {
public class TranscriptInput {
    @InvocableVariable(required=true)
    public ID routableID;
    
  }
  
  public class VisitorUrlOutput {
    @InvocableVariable(required=true)
    public String urlOfMostRecentPage;
      
  }
 
  @InvocableMethod(label='Get User URL')
  public static List<VisitorUrlOutput> getUrlName(List<TranscriptInput> transcripts) {
 
    List<VisitorUrlOutput> urls = new List<VisitorUrlOutput>();
  
    for (TranscriptInput transcript : transcripts) {
    
      // Query for the transcript record based on the ID
      LiveChatTranscript transcriptRecord = [SELECT Name, URL__c
                                             FROM LiveChatTranscript 
                                             WHERE Id = :transcript.routableID 
                                             LIMIT 1];
        
        string headerdata2= ApexPages.currentPage().getHeaders().get('Host');
        string urlvalue2=Apexpages.currentPage().getUrl();
        string url2='https://' + headerdata2+ urlvalue2;  
        
      
      // Store the url in an output variable
      VisitorUrlOutput urlData = new VisitorUrlOutput();
      urlData.urlOfMostRecentPage = url2;
      
      // Add the url to the list of outputs
      urls.add(urlData);
    }
    
    return urls;
  }
}
SwethaSwetha (Salesforce Developers) 
HI Michael,
Have you added the apex class to the sfdc.chatbot.service.permset 

Related: https://salesforce.stackexchange.com/questions/227447/calling-apex-from-einstein-bot-always-transfers-to-agent?rq=1

https://developer.salesforce.com/docs/atlas.en-us.bot_cookbook.meta/bot_cookbook/bot_cookbook_call_apex_action.htm
Fb Status in hindiFb Status in hindi
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;     
    }
}

User-added image

More More (https://www.fbstatusinhindi.xyz/2020/12/fb-status-hindi.html)
Michael MMichael M
Thank you very much for the responses. 

@Swetha, I did add it to the permission set, but it still didn't work. 

@Ishaq, I think I am doing all of those steps.

Is the problem with the way I am trying to get the URL ?

(with this:        

string headerdata2= ApexPages.currentPage().getHeaders().get('Host');
        string urlvalue2=Apexpages.currentPage().getUrl();
        string url2='https://' + headerdata2+ urlvalue2;  )
 
Michael MMichael M
Here is the code and action I am actually trying. Something must be wrong with it because the bot shuts down (just says "agent not available") when it gets to that point in my bot dialogue. Also please remember this bot is being placed on a website, and not on a community

The Apex:
User-added image

The action:
User-added image