Flow context still redirects to cancelled flow context -Cancel and Retrigger a Flow through script

Resham Noor
Tera Contributor

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

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Resham Noor 

 

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 

Screenshot 2023-04-06 at 2.50.32 PM.png

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.

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Resham Noor 

from where are you cancelling old flow and attaching new one?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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

Sandeep Rajput
Tera Patron
Tera Patron

@Resham Noor 

 

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 

Screenshot 2023-04-06 at 2.50.32 PM.png

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.

Thanks for the Update, I missed to update the RITM flow context at the end