How to trigger a flow from a UI Action

Mahesh_Krishnan
Giga Guru

All:

I have created the following UI Action to execute a flow but it is not working.

(function() {

try {
var rec_id = g_form.getUniqueValue();
var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
var inputs = {};
inputs['current'] = gr.get(rec_id); // GlideRecord of table: 
inputs['table_name'] = 'x_cpts_onboarding_cpts_referral_data';

// Start Asynchronously: Uncomment to run in background.
// sn_fd.FlowAPI.startFlow('x_cpts_onboarding.invite_to_apply', inputs);

// Execute Synchronously: Run in foreground.
sn_fd.FlowAPI.executeFlow('x_cpts_onboarding.invite_to_apply', inputs);

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

})();

The trigger condition I have set in the flow is for a record update and not create.

Per an earlier community post

https://community.servicenow.com/community?id=community_question&sys_id=d25c8167db00149c23f4a345ca9619d5

I have also set the When to Run the Flow value to "Run for non-Interactive Session", but the flow is still not triggering.

Appreciate any assistance from this group.

Mahesh

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hi, 

Try this. This is server side code snippet so please make sure you run this on the server side of UI action.

(function() {

    try {
        var rec_id = current.sys_id;
        var gr = new GlideRecord('x_cpts_onboarding_cpts_referral_data');
        if (gr.get(rec_id)) {
            var inputs = {};
            inputs['current'] = current; // GlideRecord of table: 
            inputs['table_name'] = 'x_cpts_onboarding_cpts_referral_data';

            // Start Asynchronously: Uncomment to run in background.
            // sn_fd.FlowAPI.startFlow('x_cpts_onboarding.invite_to_apply', inputs);

            // Execute Synchronously: Run in foreground.
            sn_fd.FlowAPI.executeFlow('x_cpts_onboarding.invite_to_apply', inputs);

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

})();

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

View solution in original post

8 REPLIES 8

This is great! Thank you for sharing. It wasn't obvious to me (it should have been), but I created the flow in Global but then tried to call it from a scoped app and had to include the global scope, ie. executeFlow('global.my_flow', inputs).

Community Alums
Not applicable

Hi MrMuhammad,

 

So can I run a flow without a trigger condition in this scenario?

 

Thanks,

Anu

PREVIEW
 
 
 

Supriya39
Giga Guru

I have created similar script to execute a flow with input. The flow executes fine but it does not give execution logs

Supriya39_0-1677759908304.png

Could you please help me here?

Mohd Aqib
Tera Contributor

Hello, 

Try this: This is a server-side code snippet, so please ensure you run it on the server side of the UI action.

(function() {
	
	try {
		var inputs = {};
		inputs['current'] = ; // GlideRecord of table:  
		inputs['changed_fields'] = ; // Array.Object 
		inputs['table_name'] = 'table_name';

		// Start Asynchronously: Uncomment to run in background.
		// sn_fd.FlowAPI.getRunner().flow('flow.name').inBackground().withInputs(inputs).run();
				
		// Execute Synchronously: Run in foreground.
		sn_fd.FlowAPI.getRunner().flow('flow.name').inForeground().withInputs(inputs).run();
		
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
	
})();

 

Try this: This is a client-side code snippet, so please ensure you run it on the server side of the UI action.

(function() {
	
	var inputs = {};

	inputs['current'] = { // GlideRecord 
		table : '', 
		sys_id :  
	};
	inputs['changed_fields'] = ; // Array.Object 
	inputs['table_name'] = 'tablename';

	GlideFlow.startFlow('flowbackendname', inputs)

})();

 

Thank You,
Mohd Aqib