1

I have a bot deployed using a chat button through a snap-ins chat. I've deployed this on a community using the snap-ins component and the bot engages fine.

I'm trying to get the Pre-chat form details using the technique in the Einstein Bot Cookbook for Beginners. The Apex class is as follows

public with sharing class HelpBot_GetPrechatDetails {

    public class PrechatOutput{
        @InvocableVariable(required=true)
        public String sFirstName;
        @InvocableVariable(required=true)
        public String sLastName;
        @InvocableVariable(required=true)
        public String sEmail;
        @InvocableVariable(required=true)
        public String sSubject;
    }

    public class PrechatInput{
        @InvocableVariable(required=true)
        public String sChatKey;
    }

    @InvocableMethod(label='Get SnapIns Prechat Data')
    public static List<PrechatOutput> getSnapInsPrechatData(List<PrechatInput> inputParameters)
    {
        String sChatKey = inputParameters[0].sChatKey;
        String sCaseId = null;
        String sContactId = null;

        List<PrechatOutput> outputParameters = new List<PrechatOutput>();
        PrechatOutput outputParameter = new PrechatOutput();
        if (sChatKey != null && sChatKey != '')
        {
            List<LiveChatTranscript> transcripts = [SELECT Id, CaseId,
            ContactId FROM LiveChatTranscript WHERE ChatKey = :sChatKey];
            if (transcripts.size() > 0)
            {
                sCaseId = transcripts[0].CaseId;
                sContactId = transcripts[0].ContactId;
            }
        }
        if (sCaseId != null && sCaseId != '')
        {
            List<Case> cases = [SELECT Id, Subject FROM CASE WHERE Id = :sCaseId];
            if (cases.size() > 0)
            {
                outputParameter.sSubject = cases[0].Subject;
            }
        }
        if (sContactId != null && sContactId != '')
        {
            List<Contact> contacts = [SELECT Id, FirstName, LastName, Email
            FROM Contact WHERE Id = :sContactId];
            if (contacts.size() > 0)
            {
                outputParameter.sFirstName = contacts[0].FirstName;
                outputParameter.sLastName = contacts[0].LastName;
                outputParameter.sEmail = contacts[0].Email;
            }
        }
        outputParameters.add(outputParameter);
        return outputParameters;
    }
}

I'm getting the ChatKey fine (I send it back to the user), so I test the getSnapInsPrechatData function during the bot conversation using the following code in the developer console everything works Ok; I get all the pre-chat details OK.

HelpBot_GetPrechatDetails.PrechatInput input = 
    new HelpBot_GetPrechatDetails.PrechatInput();
input.sChatKey = '5f602e2a-d6d8-42be-a92d-6420d7c57f06';

List<HelpBot_GetPrechatDetails.PrechatInput> inputs = 
    new List<HelpBot_GetPrechatDetails.PrechatInput>();
inputs.add(input);

List<HelpBot_GetPrechatDetails.PrechatOutput> results = 
        HelpBot_GetPrechatDetails.getSnapInsPrechatData(inputs);
System.debug(results[0].sFirstName);
System.debug(results[0].sLastName);
System.debug(results[0].sEmail);
System.debug(results[0].sSubject);

When it's called by the bot however it fails to find the transcript (the chatKey was passed through fine). I thought this maybe had something to do with permissions. The sfdc.chatbot.service.permset has read access to LiveChatTranscript, Case, Contact and the Apex class HelpBot_GetPrechatDetails.

Any help would be appreciated.

1

This is probably because you are using Live Agent skill based routing instead of Omni-channel queue based routing.

The key difference is that skill based routing doesn't create the LiveTranscript record until after the chat has ended while Omni-channel queue based routing will create the LiveTranscript record at the beginning of the chat.

The cookbook assumes you are using Omni-channel queue based routing for this snippet to work.

  • Thanks for your answers. I had Omni channel routing turned on. I set Default Internal Access to public read only for the Live Chat Transcript in sharing settings which worked and allowed me to continue on. I probably missed a reference to this in the documentation. There might be implications for this on production systems. – AidanD Jul 11 '18 at 8:20
  • That will do it! Glad you got it working – Matthew Mitchener Jul 11 '18 at 20:33

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.