- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 01:39 AM - edited 04-06-2023 02:00 AM
Hi All,
I am trying to cancel an existing flow and attach a new flow using script, which I was able to achieve with below code.
But I am facing an issue with the flow context ui action, when clicked it is still redirecting to the old flow which is cancelled rather than the new flow which is attached.
(function() {
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('number', 'RITM0039079'); //Update RITM Number
grRITM.query();
if (grRITM.next()) {
var ritmSysID = grRITM.getUniqueValue();
gs.print('sourceRecord: ' + ritmSysID);
gs.print("******************************************");
var grFlowContext = new GlideRecord("sys_flow_context");
grFlowContext.addQuery("source_record", ritmSysID);
grFlowContext.query();
while (grFlowContext.next()) {
gs.print("flowContextSysID: " + grFlowContext.getUniqueValue());
sn_fd.FlowAPI.cancel(grFlowContext.getUniqueValue(), 'Canceling the Flows'); // flow designer name
/*Tried setting Flow context record source field value to blank but still same
grFlowContext.source_record='';
grFlowContext.update();
*/
//CANCELLING TASK
var grApproval = new GlideRecord("sysapproval_approver");
grApproval.addEncodedQuery('state=requested^sysapproval=' + ritmSysID);
grApproval.query();
if (grApproval.next()) {
grApproval.setValue('state', 'not_required');
grApproval.update();
}
//-----------------------
/*gs.print("FlowcontextSys_id" + grFlowContext.getUniqueValue());
grFlowContext.source_record='';
grFlowContext.update();
*/
}
(function(ritm_sysID) {
try {
var inputs = {};
inputs['request_item'] = grRITM;
inputs['table_name'] = 'sc_req_item';
// GlideRecord of table: sc_req_item
// Start Asynchronously: Uncomment to run in background.
var result = sn_fd.FlowAPI.getRunner().flow('global.default_flow').inBackground().withInputs(inputs).run();
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
var contextId = result.getContextId(); //get flow context sys_id
var grFC = new GlideRecord('sys_flow_context'); //query the table sys_flow_context using the flow context sys_id to get the sys_flow_context record and populate the fields source_table and source_record
grFC.addQuery('sys_id', contextId);
grFC.query();
while (grFC.next()) {
grFC.setValue('source_table', 'sc_req_item');
grFC.setValue('source_record', ritmSysID); //sys_id of the RITM on which you want to start the flow
grFC.update();
}
})();
}
})();
UI action of flow context is OOB and is as below.
function showFlowContext () {
var url = new GlideURL('catalog_flow_context.do');
url.addParam('sysparm_sys_id', g_form.getUniqueValue());
url.addParam('sysparm_ck', g_form.getValue("sysparm_ck"));
g_navigation.open(url.getURL(), "_blank");
}
@Ankur Bawiskar @pranavbhagat @Alikutty A
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:26 AM
This is bound to happen, as you are cancelling the flow from background script and generating a new one, you also need to update the context value on the RITM record.
On the RITM record there is a field called flow_context
the same field is used when we click on the Flow context UI action in the second parameter.
url.addParam('sysparm_ck', g_form.getValue("sysparm_ck")); //flow_context value from RITM
Since you didn't update this field on the RITM hence it is still taking you to the previous context.
In your code set the value of this field like following
grRITM.setValue('flow_context’, contextId);
grRITM.update();
Hopefully this will fix your problem and you will be navigated to the correct context.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:10 AM
from where are you cancelling old flow and attaching new one?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:11 AM - edited 04-06-2023 02:28 AM
from Background script to fix the legacy records to add new flow.
https://www.servicenow.com/community/developer-forum/trigger-a-flow-from-script-source-record-has-ch... - This was the post reference used to set flow context

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:26 AM
This is bound to happen, as you are cancelling the flow from background script and generating a new one, you also need to update the context value on the RITM record.
On the RITM record there is a field called flow_context
the same field is used when we click on the Flow context UI action in the second parameter.
url.addParam('sysparm_ck', g_form.getValue("sysparm_ck")); //flow_context value from RITM
Since you didn't update this field on the RITM hence it is still taking you to the previous context.
In your code set the value of this field like following
grRITM.setValue('flow_context’, contextId);
grRITM.update();
Hopefully this will fix your problem and you will be navigated to the correct context.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 05:06 AM
Thanks for the Update, I missed to update the RITM flow context at the end