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.

Business rule to start flow designer

venkyk1309
Tera Contributor

Hi all,

I have written a script in business rule to start the flow designer 
condition after - insert & update
script :-

(function executeRule(current, previous /*null when async*/ ) {


var flowSydId = catGR.getValue('flow_designer_flow');
gs.info("&&0 " + flowSydId);

var inputs = {};
inputs.requested_item = current;
inputs.requested_item.sys_id = current.sys_id;
inputs.table_name = current.getTableName();

try {
var runner = sn_fd.FlowAPI.getRunner()
.flow(flowSydId)
.inBackground()
.withInputs(inputs)
.run();

var contextId = runner && typeof runner.getContextId === 'function' ? runner.getContextId() : '(not returned)';

gs.info("&&4 Flow triggered for Requested Item: " + current.number +
" | Flow Sys ID: " + flowSydId +
" | Context ID: " + contextId);

} catch (e) {
gs.error("&&5 Error triggering flow: " + e.message);
}


})(current, previous);

I'm able to retrieve the Flow Designer flow sys_id correctly when the catalog item is changed on a request. The issue arises when switching from an item that uses a Workflow to one that uses Flow Designer — in such cases, the RITM gets stuck and doesn't trigger approvals.

To address this, I wrote a script to manually start the Flow Designer flow when the item changes. However, it's not working as expected.

Do you have any suggestions or solutions for handling item changes where the original item runs on Workflow and the new one runs on Flow Designer? Ideally, I want the flow to trigger properly and continue the approval process without getting stuck.

1 REPLY 1

G Ponsekar
Tera Guru

Hi @venkyk1309 ,

 

The fundamental problem is likely that the RITM already has an active or completed workflow context attached from the original catalog item. When you change the cat_item and try to manually start a Flow Designer flow via a Business Rule, the platform gets confused because the RITM record is still linked to the old execution path.

 

Try updating script like below:

  • Condition: current.cat_item.changes()
  • When: after update
  • Order: 1000
(function executeRule(current, previous /*null when async*/ ) {

    // 1. Define the function to cancel existing workflows
    function cancelExistingWorkflow(ritmSysId) {
        var wf = new Workflow();
        var contextGr = new GlideRecord('wf_context');
        contextGr.addQuery('id', ritmSysId);
        contextGr.addQuery('state', 'executing');
        contextGr.setLimit(1);
        contextGr.query();
        if (contextGr.next()) {
            gs.info("&&0 Canceling existing workflow context: " + contextGr.sys_id + " for RITM: " + ritmSysId);
            wf.cancel(contextGr.sys_id);
            // Give the cancellation a moment to process before starting the new flow            
gs.sleep(
500); } } // 2. Main Logic: Check if the new catalog item uses a Flow Designer flow var catGR = new GlideRecord('sc_cat_item'); if (!catGR.get(current.cat_item)) { return; // Exit if the new catalog item can't be found
}
var flowSysId = catGR.getValue('flow_designer_flow'); if (flowSysId) { // If the new item uses a flow, cancel the old workflow first
cancelExistingWorkflow(current.sys_id);
// 3. Start the new Flow Designer flow var inputs = {}; // Pass the RITM record correctly as input
inputs.requested_item = current;
try { var runner = sn_fd.FlowAPI.getRunner() .flow(flowSysId) .inBackground() .withInputs(inputs) .run(); var contextId = runner && typeof runner.getContextId === 'function' ? runner.getContextId() : '(not returned)'; gs.info("&&4 Flow triggered for Requested Item: " + current.number + " | Flow Sys ID: " + flowSysId + " | Context ID: " + contextId); } catch (e) { gs.error("&&5 Error triggering flow via Business Rule: " + e.message); } } })(current, previous);

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP