Skip to main content

ServiceNow Executing a Flow Action via Script (Async and Sync)

 ServiceNow Executing a Flow Action via Script (Async and Sync)

Diff between asynch v synch

//-- Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.

                //-- sn_fd.FlowAPI.getRunner().action('global.glidepath__create_sso').inBackground().withInputs(inputs).run();

 

                //-- Execute Synchronously: Run in foreground. Code snippet has access to outputs.

                var result = sn_fd.FlowAPI.getRunner().action('global.glidepath__create_action').inForeground().withInputs(inputs).run();

                var outputs = result.getOutputs();

 

                //-- Get Outputs:

                //-- Note: outputs can only be retrieved when executing synchronously.

                var response_body = outputs['response_body']; // String

                var response_error = outputs['response_error']; // String

                var response_status_code = outputs['response_status_code']; // String

                var response_error_code = outputs['response_error_code']; // String

                var dag_run_id = outputs['dag_run_id']; // String

 

In this case it’s running a flow action – table name is sys_hub_action_type_definition

Value highlighted is internal name

From: "Smith, Ruen (NonEmp)" <ruen.smith@kroger.com>
Date: Wednesday, 11 March 2026 at 08:37
To: Ruen Smith <ruendsmith@hotmail.com>
Subject: Execute flow synchronously

 

var inputs = {};

                inputs['request_payload'] = requestPayload; // JSON

                inputs['servicenow_id'] = cat_task_sys_id; // String

                inputs['dag_run'] = automation_flow_name; // String

                inputs['endpoint_server'] = endpoint_server; // String

                inputs['token'] = bearer_token; // String

 

 

//-- Execute Synchronously: Run in foreground. Code snippet has access to outputs.

                var result = sn_fd.FlowAPI.getRunner().action('global.glidepath__create_action').inForeground().withInputs(inputs).run();

                var outputs = result.getOutputs();

 

//-- Get Outputs:

                //-- Note: outputs can only be retrieved when executing synchronously.

                var response_body = outputs['response_body']; // String

                var response_error = outputs['response_error']; // String

                var response_status_code = outputs['response_status_code']; // String

                var response_error_code = outputs['response_error_code']; // String

                var dag_run_id = outputs['dag_run_id']; // String

 



 

 

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...