Sitemap
Santiago T

Santiago T

I am an IT Professional happy to share my knowledge and learn new things :)

Salesforce Einstein Bots: How to send data to an Einstein Bot before starting a conversation (Enhanced and Standard)

7 min readAug 14, 2023

--

Having recently worked in a Service Cloud project we had the need to send data to a Salesforce Einstein Bot automatically to fulfill a particular business case (More details about it below), with this post I want to share a guide on how this can be achieved with both an Enhanced and an Standard implementation (Standard implementation details are added as an annex at the end), hopefully you will find it useful for your own implementations.

Business Case: The bot needs to start-up on a different dialog depending on the button it is being called from, this inevitably includes a development portion but from a configuration standpoint we can achieve 90% of the goal.

The methods varies as I mentioned previously, I will start with the Enhanced implementation.

  • Assumptions: You have a chat button already configured and running and it is correctly connected to an Einstein Bot. My set-up has the Embedded deployment added to a VisualForce Page within my Salesforce Org for testing purposes, basically just pasted the code snippet Salesforce generates for you in an empty VisualForce Page.

Enhanced Channel implementation

  1. Head over to Set Up > Messaging Settings and click over your Messaging in-App and Web (MiAW) channel’s name, this will open up the Messaging Settings screen:
Press enter or click to view image in full size
Messaging Settings Screen

2. Create a new custom parameter to store a value that will later let us define the dialog that needs to be opened, custom parameters are meant to be used along with Pre-chat forms, more on that later. Click the “New” button in the right to create a custom parameter, by default the parameter’s name, API name and channel variable name will be the same, it is recommended to leave them as is, define a maximum length, an example of how mine looks:

Press enter or click to view image in full size
Custom paramete section

3. Create a new parameter mapping, this will “connect” our parameter to a Flow that will enable us to check the value and modify the behavior of the bot as required, click the “New” button, select the custom parameter we created in the previous step and give a name to the Flow variable name, for the sake of simplicity I will use the same name.

Press enter or click to view image in full size
Mapping of the custom parameter

This flow variable works the same way as the well known “recordId” variable does, in order to use it go to the Omni-Flow you have defined for your channel and create a new variable available for input with the exact same name, with this every time a conversation enters through web chat and if a value is sent to the custom parameter it will be captured by the Flow

Press enter or click to view image in full size
Creating flow variables for our custom parameters

You can then add an “Update record” to your Omni-Flow to store the value of the custom parameter:

Press enter or click to view image in full size

Official Salesforce documentation detailing the process briefly explained above

4. Now we need to enable Pre-chat forms for our MiAW button, head over Setup > Embedded Service Deployments and click over the more actions menu > View:

Press enter or click to view image in full size

That will display the following screen

Press enter or click to view image in full size
MiAW Web Deployment Settings page

5. Click on Edit Pre-Chat > Check “Activate the pre-chat feature” checkbox

6. Scroll down to the “Hidden Pre-Chat Fields” and move the parameter we created in Step 2, save the configuration by clicking on the Save button in the bottom right corner:

Press enter or click to view image in full size
Adding our custom parameters as hidden fields

This will take you back to the Embedded Deployment’s settings screen, over the top right corner click “Publish”:

Bear in mind that this will send an email to all Salesforce users when the site is ready as MiAW uses an Experience Cloud site, to circumvent users receiving this email you can go to Set up > Deliverability > Access Level: No access

Disable emailing for all users

You can then go back to the embedded mesagging settings page and keep updating until the “Published on” date is updated, at that point you can re-enable Deliverability Access level back to normal, publishing usually takes a few minutes.

7. We now need to create a field to save the value that will be fed to the bot, go to Object Manager > Messaging Session and create a new field, use the same Name we have used before, it is also important that the Field type = String (Text) and the field length matches the one defined in Step 2.

Press enter or click to view image in full size
Field created to store custom parameter value

8. Now we will create a Flow to read the value in the “openingDialog” field and feed the value back to the bot, head over Set Up > Flow > New Flow and create a new Auto-launched flow.

Get Santiago T’s stories in your inbox

Join Medium for free to get updates from this writer.

9. Create two variables, “recordId “(Don’t forget to make it available for input), and an additional variable, marked available for output, this is what we will return to the Bot

Press enter or click to view image in full size

10. Create a Get Records on the Messaging Session object with the following configuration:

Press enter or click to view image in full size
Press enter or click to view image in full size

Save and activate the Flow.

11. Now we go into the Einstein Bot, Welcome Dialog, as it is the first dialog that is executed, here we will use the Flow to determine which dialog to open next, Add a new Flow Component:

Next, select the Flow we created in the previous step, map the “Routable Id” variable to the “recordId” the Flow expects to receive and create a new text type variable to store the value the Flow returns:

Now we can use “dialog” in rules to launch the specific dialog we want -or execute any other action we might need-:

Right after the Flow is executed I defined a rule that will redirect to a particular dialog, if the paremeter sends something different to “Dialog1” the Bot will follow the usual welcome path

Before we are able to test everything there is still one last addition to our configuration, we need to add a couple of additional lines to the Code Snippet Salesforce automatically generates for us to deploy the Chat into a web page. Add it just above “embeddedservice_bootstrap.init(…)”:

window.addEventListener("onEmbeddedMessagingReady", () => {
console.log("Received the onEmbeddedMessagingReady event…");
embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({"Name of Pre-Chat Field" : "Value we want to send", "Name of Pre-Chat Field 2" : "Value we want to send 2"});
});

You can also send variables as values not just plain values, but that enters in web development territory, not in the scope of this guide.

It would look something like this:

Press enter or click to view image in full size

Official Salesforce documentation for APIs that can be used with MiAW channels

Results:

Behavior if I send “Nada” (“Nothing”):

Bot follows default path defined

Behavior if I send “Dialog1” as value:

Bot redirects on start-up to a different dialog

Common issues I ran while testing:

  1. I did all the steps above and even though I am sending a value through the web page it is not being stored in the Messaging Session: Check if the value you are sending exceeds the field’s length.

Annex: Standard Web Channel Implementation

Implementing a similar behavior in standard Web Chat implementations can be achieved albeit with a few differences.

  1. Custom field needs to be created in the “Chat Transcript” object instead of the Messaging Session object:
Press enter or click to view image in full size
“test” field created in Chat Transcript Object

2. API used for standard web chat is different, click here to review it

Basically I added to my standard web deployment snippet the following line:

embedded_svc.settings.extraPrechatFormDetails = [{"label":"Test", "value":"Dialog1", "transcriptFields": ["test__c"]}];

Add it right above the “embedded_svc.init()” section of the snippet

And that is it! Standard implementation is pretty straight-forward, as we are already declaring the field’s API name within the code itself, so when we launch our chat and the conversation is finalized, the field will automatically populate with the value that was sent:

Value passed through web page is saved directly to the Chat Transcript instance

Update log:

03/11: Minor spelling corrections

--

--