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

Community site session details

Session Id :
Copilot Studio - Calling Actions from Copilot S...
Answered

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

(0) ShareShare
ReportReport
Posted on by 14

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);
});
 
Categories:
I have the same question (0)
  • Verified answer
    SwatiSTW Profile Picture
    671 Super User 2025 Season 2 on at
    set variable from browser to Copilot studio - - REDUX / direct line API
    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
  • Anilal Profile Picture
    14 on at
    set variable from browser to Copilot studio - - REDUX / direct line API
    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Telen Wang – Community Spotlight

We are honored to recognize Telen Wang as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Copilot Studio

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 468 Super User 2025 Season 2

#2
stampcoin Profile Picture

stampcoin 52 Super User 2025 Season 2

#3
trice602 Profile Picture

trice602 46 Super User 2025 Season 2

Featured topics