I wrote a simple InvocableMethod to increment the amount of times an Einstein chatbot answers a users message. This works sometimes, very inconsistently. Sometimes it fails right away when I call a dialog that calls the apex class. Other times it will allow me to pass through the dialog with the apex 2 or 3 times. I think it has to do with the bot reusing variables without clearing them, but I am hoping someone here can give me a better answer.

It seems to only work when I pass strings and convert to integer, seems the bot can only work with strings:

public class ResponsesInput {
    public String numOfResponses = '';

public class ResponsesOutput {
    public String botResponses = '';

@InvocableMethod(label='Increment Bot Response')
public static List<ResponsesOutput> incrementBotResponse(List<ResponsesInput> responsesInput) {
    //Get the # of responses from the input
    Integer existingResponses;
    for (ResponsesInput respInput : responsesInput) {
            existingResponses = Integer.valueOf(respInput.numOfResponses);
            existingResponses = 0;

    //Increment # of responses and return to bot
    List<ResponsesOutput> outputParameters = new List<ResponsesOutput>();
    ResponsesOutput outputParameter = new ResponsesOutput();
    Integer outPutNumOfResponses = existingResponses + 1;
    outputParameter.botResponses = String.valueOf(outPutNumOfResponses);

    return outputParameters;

Here is the call to the dialog to start the class because I want it to return to this dialog when complete: Bot builder dialog to call apex dialog

Here is the actual dialog that calls this class: dialog apex class


I would assume that you're right about variable cleaning. Clear them out like this before entering the action: Clear vars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.