web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Copilot Studio / Setting variables with...
Copilot Studio
Unanswered

Setting variables with javascript

(0) ShareShare
ReportReport
Posted on by 74

Hi, 


We are setting some variables via javascript with our bot. It works, except that because of what seems to be an asynchronous code execution issue, the startConversation topic often doesn't have access to the variables before it launches. I could put in a delay in javascript as kind of a hack, but that isn't really desirable. I'm hoping there's a more elegant and simple solution.

Here is the current code:

 const store = WebChat.createStore({}, ({ dispatch }) => next => action => {
 if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
 dispatch({
 type: "WEB_CHAT/SEND_EVENT",
 payload: {
 name: "pvaSetContext",
 value: {
 "orgType": "LCM",
 "websiteName": "testWebsite"
 }
 },
 });

 // Add the startConversation event here
 directLine
 .postActivity({
 localTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
 locale,
 name: 'startConversation',
 type: 'event'
 })
 .subscribe();
 }

 return next(action);
});


It has the variable in startConversation about 50% of the time. Which we're using to change the first message the user sees depending on the website. Open to solutions other than this as well. We initially used the querystring variation, but then added some other javascript to trigger an event that resets the conversation. We may be able to achieve this another way with refreshing an iframe possibly and then we could use the querystring version. But if there's a way to do it with the javascript that I haven't figured out, that would be great. 

Thanks for any tips!
Wesley

Screenshot 2024-02-23 at 3.01.10 PM.png

Categories:
I have the same question (0)
  • adilei Profile Picture
    Microsoft Employee on at

    Interesting. Can you share your "Start Conversation" YAML?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Copilot Studio

#1
Valantis Profile Picture

Valantis 619

#2
chiaraalina Profile Picture

chiaraalina 163 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 147

Last 30 days Overall leaderboard