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 / set variable from brow...
Copilot Studio
Answered

set variable from browser to Copilot studio - - REDUX / direct line API

(0) ShareShare
ReportReport
Posted on by 49

Hi Experts,

i have Global variable set in copilot studio
    - kind: SetVariable
      id: setVariable_OkLu9i
      variable: Global.VarUserType
      value: Current Employee
      
      
      
 I am using Webchat.js, i need to set the value of that Variable based on the radio button selection which is happening in the browser chat window.
I tried the below code, but getting the errror message as below in Browser console.
 
Any thoughts highly appreciated.


Error:
webchat.js:2 TypeError: Cannot read properties of undefined (reading 'method')
    at webchat.js:2:2364778
    at webchat.js:2:2366409
    at Generator.next (<anonymous>)
    at p (webchat.js:2:2301636)
    at yt (webchat.js:2:2301466)
    at webchat.js:2:2298085
    at Ue (webchat.js:2:2294719)
    at nt.<computed> (webchat.js:2:2298064)
    at webchat.js:2:2301270
    at d (webchat.js:2:2302170)
onError @ webchat.js:2



Code Snippet to update the Global variable set in Copilot studio. Selected the check box in the copilot UI to allow external to make modification.

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
  try {
    if (
      action.type === "DIRECT_LINE/POST_ACTIVITY" &&
      action.payload &&
      action.payload.activity
    ) {
      const activity = action.payload.activity;
      if (activity?.value?.actionType === "submitEmploymentStatus") {
        const selectedStatus = activity.value.employmentStatus;
        // Send event to Copilot Studio to set global variable
        dispatch({
          type: "DIRECT_LINE/POST_ACTIVITY",
          payload: {
            activity: {
              type: "event",
              name: "SetVariable",
              value: {
                name: "Global.VarUserType",
                value: selectedStatus
              }
            }
          }
        });
        console.log(`Global.VarUserType set to ${selectedStatus}`);
      }
    }
  } catch (err) {
    console.error("Error in WebChat middleware:", err);
  }

  return next(action);
});
 
I have the same question (0)
  • Anilal Profile Picture
    49 on at
    Update - followed this below MS link,  still not setting the variable properly,

    https://learn.microsoft.com/en-us/microsoft-copilot-studio/guidance/pass-context-variables-from-webpage-to-copilot
     
    Any one here tried , kindly share some lights to this,
     
    Anilal
  • Verified answer
    SwatiSTW Profile Picture
    809 Super User 2026 Season 1 on at
    Hi @Anilal,
     
    You want to set a Copilot Studio global variable from Web Chat after a radio selection. Posting a custom SetVariable event with DIRECT_LINE/POST_ACTIVITY is unsupported and causes the Web Chat error.
    1. use pvaSetContext and send it with WEB_CHAT/SEND_EVENT, not DIRECT_LINE/POST_ACTIVITY
    2. remove the Global. prefix when sending the variable name; send VarUserType for Global.VarUserType
    3. keep your middleware and, on submit, dispatch pvaSetContext like this
    const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
      if (
        action.type === 'DIRECT_LINE/POST_ACTIVITY' &&
        action.payload?.activity?.value?.actionType === 'submitEmploymentStatus'
      ) {
        const selectedStatus = action.payload.activity.value.employmentStatus;
        dispatch({
          type: 'WEB_CHAT/SEND_EVENT',
          payload: {
            name: 'pvaSetContext',
            value: { VarUserType: selectedStatus } // no "Global."
          }
        });
        console.log('Global.VarUserType ->', selectedStatus);
      }
      return next(action);
    });
    4. in Copilot Studio mark the variable as Global and allow External sources can set values; if you read it early, add Get value from this node if empty and a short timeout
    5. if you prefer to set it on connect, send pvaSetContext on DIRECT_LINE/CONNECT_FULFILLED
    const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
      if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        dispatch({
          type: 'WEB_CHAT/SEND_EVENT',
          payload: { name: 'pvaSetContext', value: { VarUserType: 'Current Employee' } }
        });
      }
      return next(action);
    });
    6. render Web Chat with the same store

    window.WebChat.renderWebChat(
      { directLine: window.WebChat.createDirectLine({ token }), store },
      document.getElementById('webchat')
    );
    7. test by reading the variable in a message or condition; it should resolve after pvaSetContext arrives or fall back to your default after the timeout

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 April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Copilot Studio

#1
Valantis Profile Picture

Valantis 634

#2
Vish WR Profile Picture

Vish WR 315

#3
Haque Profile Picture

Haque 219

Last 30 days Overall leaderboard