ScriptableFlowRunner used in Workflow to get approvals

Matt Grover1
Giga Contributor

Hello,

I am working with a requirement that any manager level approvals need to be sent to a full-time employee rather than a contractor.  Since we are adopting flow designer for new catalog items, I built out a subflow/action in flow designer to ensure the approval is routed to an FTE.  This works fine for catalog items that use flow designer.

However, we do have some legacy catalog items that still use workflow editor.  I recently learned about the ability to call a flow/subflow directly from a run script in workflow editor using the sn_fd.ScriptableFlowRunner API, which I prefer doing over creating parallel logic directly in workflow because this allows us a single point of control for any changes to the approval subflow.  This is the code I've used, which I've modified from the API sample code:

(function() {
    try {

        var inputs = {};
		inputs['requested_for'] = current.request.requested_for.sys_id.toString();
        inputs['requested_item'] = current.sys_id.toString();
		
		gs.info("Requested For: " + inputs.requested_for.toString());
		gs.info("Requested Item: " + inputs.requested_item.toString());

        var result = sn_fd.FlowAPI.getRunner() // Create a ScriptableFlowRunner builder object.
            .subflow('global.fte_manager_approval') // Run the global scope subflow fte_manager_approval.
            .withInputs(inputs)
			.inForeground()
            .run(); // Run the action and return a FlowRunnerResult object.

		gs.info("Result: " + result.toString());
        var contextId = result.getContextId(); // Retrieve the context ID from the result
        var dateRun = result.getDate();
        //var domainUsed = result.getDomainId(); // Retrieve the Domain ID from the result.
        var flowName = result.getFlowObjectName();
        var flowObjectType = result.getFlowObjectType();

        var outputs = result.getOutputs(); // Retrieve any outputs from the action execution.
		gs.info('Approval State: ' + outputs['approval_state']);
		gs.info('Outputs: ' + outputs);
        
		workflow.scratchpad.status = outputs['approval_state'];
		

    } catch (ex) {
        var message = ex.getMessage();
		gs.error("Catch block triggered");
        gs.error(message);
    }

})();

 

The approval gets generated to the correct manager user, however, it appears that this triggers the catch block error message, and the workflow doesn't pause until the approval subflow is completed, which is what I thought the .inForeGround() parameter was used for.  It moves on to the next action without getting the outputs of my subflow:

find_real_file.png

 

Does anyone know how to properly call this API in the middle of a workflow so that you can determine the output of an approval subflow?

1 ACCEPTED SOLUTION

Matt Grover1
Giga Contributor

Update on this issue.  I had to re-code my flow so that the approval itself was happening in the main workflow instead of being contained in the subflow.  Calling the subflow in the main workflow editor never was able to properly wait for the approval to be returned, so I just pulled that into the main workflow.  Other than that, I was able to get the flow API working.

View solution in original post

2 REPLIES 2

Wayne C
Tera Contributor

ScriptableFlowRunner has a very short default timeout value; it's likely just timing out and allowing your workflow to continue.

If that's the case, you need to add

   .timeout(valueInMilliseconds)

somewhere in your getRunner() code block.

Matt Grover1
Giga Contributor

Update on this issue.  I had to re-code my flow so that the approval itself was happening in the main workflow instead of being contained in the subflow.  Calling the subflow in the main workflow editor never was able to properly wait for the approval to be returned, so I just pulled that into the main workflow.  Other than that, I was able to get the flow API working.