Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue in retriggering Flow via Business Rule

zee15
Tera Contributor

Hi everyone,

I'm working on a use case where I need to re-trigger a flow using a Business Rule, but I'm currently stuck.

 

Use Case Overview:
On a catalog item form, there's a field called Request Type with two choices: Direct and Indirect.


Here's the scenario:

  1. A user submits a request using Direct as the type.
  2. The RITM goes for review.
  3. During the review, the reviewer changes the Request Type from Direct to Indirect.

At this point, I want to cancel the existing flow and trigger a new flow that is attached to the same RITM.

I'm using the following script to achieve this:

var grFlowContext = new GlideRecord("sys_flow_context");
grFlowContext.addEncodedQuery("name=Catalog Item:testing_flow^source_record=3f740119976dee10876eb376f053af65");
grFlowContext.orderByDesc("sys_created_on");
grFlowContext.query();

if (grFlowContext.next()) {
    //Cancel existing flow context
    sn_fd.FlowAPI.cancel(grFlowContext.sys_id.toString(), 'Request Type Change');
}

var flow_context_id = ""
var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("numberSTARTSWITHRITM0497885");
gr.query();
if (gr.next()) {
    try {
        var inputs = {};
        inputs['table_name'] = 'sc_req_item';
        inputs['request_item'] = gr; // GlideRecord of table: sc_req_item 
		gs.info("running...........")

        // Execute Synchronously: Run in foreground.
        var result = sn_fd.FlowAPI.getRunner().flow('global.testing_flow').inForeground().withInputs(inputs).run();
		flow_context_id = result.getContextId(); 
        gs.info("########### flow_context_id : ");


    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }
}

var grFlowtrigger = new GlideRecord("sys_flow_context");
if (grFlowtrigger.get(flow_context_id)) {
    grFlowtrigger.source_table = "sc_req_item";
    grFlowtrigger.source_record = gr.getValue("sys_id");
    grFlowtrigger.update();
}

The script is working in terms of:

  • Successfully cancelling the previous flow.
  • Creating a new flow.

Issue:
The new flow is not attaching to the RITM. The source_table and source_record fields in sys_flow_context remain empty. Check the ss.

zee15_1-1747391136973.png

 

I'm trying to update them manually after the flow is triggered, but I'm unable to get the correct sys_id of the newly running flow context.

Can anyone please advise how I can properly attach the newly triggered flow to the RITM?

Thanks in advance for your help!

 

2 REPLIES 2

Abbas_5
Tera Sage
Tera Sage

Hello @zee15,

 

For similar kind of issue, please refer to the below link:
https://www.servicenow.com/community/developer-forum/how-to-trigger-flow-via-business-rule/m-p/26356...

 

If it is helpful, please mark it as helpful and accept the correct solution by refer to this solution it will helpful to them.

 

Thanks & Regards,

Abbas Shaik

zee15
Tera Contributor

Hi,

I successfully triggered the flow using a Business Rule. However, I encountered an error that appeared only once but was visible across different areas:

zee15_0-1748329338726.png

So these errors are visible:

  • On the portal during record creation
  • While creating an update set
  • When other team members created RITMs

Interestingly, the same one-time error occurred in UAT after I pushed the update set. It's no longer showing up in either environment.

Does anyone have insight into why this might have happened?

Thanks in advance for your help!