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

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;
})();