How to launch a flow from a script

David Post
Kilo Expert

I'm failing to launch a flow from a Run Script activity. My goal is to use this Run Script action (in a Workflow) to launch the Flow, so that the Flow can do what it does.

Run Script action:

try {
    var inputs = {};
    inputs['table_name'] = 'sc_req_item';
    inputs['request_item'] = current.sys_id; // GlideRecord of table: sc_req_item 
	workflow.info('Requested Item: ' + current.number + ', sys_id: ' + current.sys_id);

    // Execute Synchronously: Run in foreground.
    sn_fd.FlowAPI.getRunner().flow('global.create_user').inForeground().withInputs(inputs).run();
	workflow.info('Launched "Create User" flow in Flow Designer');

} catch (ex) {
    var message = ex.getMessage();
	workflow.info("Failed to launch the Flow in Flow Designer: " + message);
    gs.error(message);
}

What I'm getting back in the workflow log is:

find_real_file.png

What do I need to feed the inputs['request_item'] for this to work?

1 ACCEPTED SOLUTION

David Post
Kilo Expert

To successfully launch a flow (or action) from Flow Designer, use the entire code snippet provided by Flow Designer in your action. From there, you can use current to populate the inputs['request_item'] value:

var inputs = {};
inputs['table_name'] = 'sc_req_item'; // This starts as 'Table Name', and you have to set it.
inputs['request_item'] = current; //GlideRecord of table: sc_req_item

Where you'll run into issues is if you aren't actually handling the possible responses from the overall flow execution; avoid logging in the catch part of the try/catch block because it'll hit that part while the flow is waiting to complete (if you have a single 'wait' activity in there).

View solution in original post

2 REPLIES 2

David Post
Kilo Expert

tried this:

var currentReq = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.sys_id);
gr.query();

try {
    var inputs = {};
    inputs['table_name'] = 'sc_req_item';
    inputs['request_item'] = currentReq; // GlideRecord of table: sc_req_item 
	workflow.info('Requested Item: ' + current.number + ', sys_id: ' + current.sys_id);

    // Execute Synchronously: Run in foreground.
    sn_fd.FlowAPI.getRunner().flow('global.create_user').inForeground().withInputs(inputs).run();
	workflow.info('Launched "Create User" flow in Flow Designer');

} catch (ex) {
    var message = ex.getMessage();
	workflow.info("Failed to launch the Flow in Flow Designer: " + message);
    gs.error(message);
}

but it resulted in a more generic error:

David Post
Kilo Expert

To successfully launch a flow (or action) from Flow Designer, use the entire code snippet provided by Flow Designer in your action. From there, you can use current to populate the inputs['request_item'] value:

var inputs = {};
inputs['table_name'] = 'sc_req_item'; // This starts as 'Table Name', and you have to set it.
inputs['request_item'] = current; //GlideRecord of table: sc_req_item

Where you'll run into issues is if you aren't actually handling the possible responses from the overall flow execution; avoid logging in the catch part of the try/catch block because it'll hit that part while the flow is waiting to complete (if you have a single 'wait' activity in there).