Cancel workflow script is not working in VA

siva44
Tera Contributor

I have created one topic for open RITMS and given actions for cancel RITM for cancel i have write the below script in VA but not working state and comments updating fine but stage and workflow cancel not working

 

script:


(function execute() {
var response = '';
var gr = new GlideRecordSecure(vaVars.table_name);
if (gr.get(vaVars.sys_id)) {
 
var comment=vaInputs.reason_for_request_cancel.getValue().trim()
if (comment) {
var workflow = new global.Workflow();
workflow.cancel(gr);
gr.approval = 'withdrawn';
 gr.state = 4;
gr.stage= 'Request Cancelled';
gr.comments="Cancelled by with comment: " + comment;
gr.active = false;
gr.update();
if (vaVars.table_name == 'sc_req_item')
response = gs.getMessage("Requested item has been cancelled");
 
else {
response = gs.getMessage("OK, no comment will be added.");
}
} else
response = gs.getMessage("I couldn't find the record.");
}

return response;
})()


1 REPLY 1

Ravi Gaurav
Giga Sage
Giga Sage

Hi @siva44 

Got it you’re cancelling an RITM from a Virtual Agent action.
Your script is updating some fields fine (state, comments) but stage + workflow cancel isn’t taking effect.

 

(function execute() {
var response = '';
var gr = new GlideRecordSecure(vaVars.table_name);

if (gr.get(vaVars.sys_id)) {
var comment = vaInputs.reason_for_request_cancel.getValue().trim();

if (comment) {
// Cancel workflows first
var wf = new global.Workflow();
wf.cancel(gr);

// Update RITM fields
gr.state = 4; // Closed Incomplete
gr.stage = 'request_cancelled'; // use internal value, not label
gr.approval = 'withdrawn';
gr.comments = "Cancelled by VA with comment: " + comment;
gr.active = false;

gr.update();

response = (vaVars.table_name == 'sc_req_item') ?
gs.getMessage("Requested item has been cancelled") :
gs.getMessage("OK, no comment will be added.");
} else {
response = gs.getMessage("No cancel comment provided.");
}
} else {
response = gs.getMessage("I couldn't find the record.");
}

return response;
})();

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/