0

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 {
    @InvocableVariable(required=false)
    public String numOfResponses = '';
}

public class ResponsesOutput {
    @InvocableVariable(required=false)
    public String botResponses = '';
}


@InvocableMethod(label='Increment Bot Response')
public static List<ResponsesOutput> incrementBotResponse(List<ResponsesInput> responsesInput) {
    system.debug(responsesInput);
    //Get the # of responses from the input
    Integer existingResponses;
    for (ResponsesInput respInput : responsesInput) {
        if(!String.isBlank(respInput.numOfResponses)){
            existingResponses = Integer.valueOf(respInput.numOfResponses);
        }else{
            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);
    outputParameters.add(outputParameter);

    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

Your Answer

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

Browse other questions tagged or ask your own question.