We have an embedded service chat in Experience cloud community page. We are populating the fields on chat transcript object using the 'snippet' file as mentioned here:
Skip for those who know how snippet settings file and bot context variables work
The values in these fields are populated by taking them from session storage. These values are set in the session storage through some action in another component in the same community page. I have referred this blog for developing this functionality, except for VF page used LWC. https://www.infallibletechie.com/2023/09/greet-customers-in-salesforce-einstein-bot-without-pre-chat-from-experience-cloud-site.html
When the Community page loads first time and bot is opened, snippet file is invoked and the chat transcript object fields are successfully populated on record. This helps in linking and setting context variable with chat transcript field as mentioned in the blog, and decision making in the chatbot flow logic can be done using this variable.
Actual problem
The problem now is that when following happens: one session of chat is ended (End and close chat) -> user makes some modification in the other component -> session storage is modified with new value -> Bot is opened again.
This time (unlike first time when page was reloaded) the snippet file is not invoked at all. Although a new record of chat transcript is created, since snippet file is not invoked, the updated values are not set in the chat transcript record, thus affecting chatbot flow logic.
I have tried many things which include:
- Using
embedded_svc.init
method - Adding event listener in snippet file and on session storage change invoking the
embedded_svc.snippetSettingsFile.extraPrechatFormDetails
code. - Using standard events of prechat API like 'onChatRequestSuccess', 'onQueueUpdate'.
None of the above works. Although the code of embedded_svc.snippetSettingsFile.extraPrechatFormDetails
is run even before chat transcript record is created, the record is not populated with the values set in the code.
Please find snippet file code here
window._snapinsSnippetSettingsFile = (function () {
let userArea = window.sessionStorage.getItem('externalUserArea');
embedded_svc.snippetSettingsFile.extraPrechatFormDetails = [{
"label" : "User Area",
"value" : userArea,
"transcriptFields" : ["User_Area__c"]},
];
console.log('Array : ' + JSON.stringify(embedded_svc.snippetSettingsFile.extraPrechatFormDetails ));
})();```
[1]: https://www.infallibletechie.com/2023/09/greet-customers-in-salesforce-einstein-bot-without-pre-chat-from-experience-cloud-site.html
[2]: https://developer.salesforce.com/docs/atlas.en-us.snapins_web_dev.meta/snapins_web_dev/snapins_web_snippet_settings_file.htm