How to use FlowScriptAPI.getContextID() in script includes

Rajyalakshmi
Giga Guru

How to use FlowScriptAPI.getContextID() in script includes

5 REPLIES 5

glideFather
Tera Patron

ahoy @Rajyalakshmi,

 

you can see an example of that usage at FlowAPI - Scoped, Global


✂-----Cutting-out-the---✦AI-noise✦---All-replies-written-and-vouched-for-by-GlideFather---

KartikM49574975
Kilo Sage

Hello @Rajyalakshmi 

FlowScriptAPI can't be instantiated, and it is only available in Flow script steps and inline scripts. It isn't injected into a script include's scope, so calling it there will fail. I haven't run this myself, but that is what the documentation implies. [servicenow.com]

Instead, get the context ID in the script step and pass it to the script include as a parameter.

 

Script step (Flow action):

 

(function execute(inputs, outputs) {
    var contextId = FlowScriptAPI.getContextID(); // sys_id of the running flow context
    var util = new global.MyFlowUtil();
    outputs.result = util.doSomething(contextId, inputs.some_input);
})(inputs, outputs);

 

Script include:

 

var MyFlowUtil = Class.create();
MyFlowUtil.prototype = {
    initialize: function() {},
 
    doSomething: function(contextId, someInput) {
        gs.info('Flow context: ' + contextId);
        // Use contextId, e.g. to query sys_flow_context or sys_flow_log
        return 'done';
    },
 
    type: 'MyFlowUtil'
};

 

getContextID() takes no parameters and returns a String, the sys_id of the running flow. [servicenow.com]

 

If your script include is triggered from outside a flow, such as a business rule that starts a flow, use FlowAPI instead. The runner's run() result has a getContextId() method. [servicenow.com]

 

var result = sn_fd.FlowAPI.getRunner()
    .flow('global.myFlow')
    .inBackground()
    .withInputs(inputs)
    .run();
var contextId = result.getContextId();

 

Thanks.

vaishali231
Mega Sage

Hey @Rajyalakshmi 

FlowScriptAPI.getContextID() returns the sys_id of the current Flow execution context.

It can be used in Flow Script steps and inline scripts. If you need the context ID in a Script Include, get it from the Flow script and pass it to the Script Include as a parameter.

var contextId = FlowScriptAPI.getContextID();
var helper = new MyFlowHelper();
helper.process(contextId);

This way, the Script Include receives the Flow context ID without directly depending on FlowScriptAPI.

 

***********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb




Ankur Bawiskar
Tera Patron

@Rajyalakshmi 

what's your business requirement?

where are you stuck?

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