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