venkatiyer1
Giga Guru

Recently, I worked on the Parallel Flow Activity which works like a charm. I was using a catalog item i.e. sc_req_item table and calling multiple sub flows. I couldn't find lot of information on it so I am writing this post so that others find it useful. The situation I came across was on a catalog item which had to make multiple rest calls and each rest call could have its own separate workflow involving tasks and error logs etc. Having its own subflow makes it reusable for other workflows. Initial documentation which is a very good starting point is linked here below:-

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/administer/workflow-activities/r...

While using Parallel Flow Launcher either you could call the same workflow multiple times by giving it a series of inputs or call multiple workflows based on conditions within your form.

If you are using a single workflow, I believe it is straightforward as choosing the workflow in the workflow selection and adding the inputs as an array. The number of inputs would determine the number of subflows that get generated. To get the input values into your sub flow you would have to edit inputs add a new input with the same key as below and define a column type accordingly.

Initial.png

But if you are wanting to use multiple workflows, you would have to click the Advanced checkbox and create the coordinator object :-

So within the Workflow field, you can add the following script as shown in the screenshot. Workflow1 and Workflow2 are the name of the Workflows you would like to call.

Coordinator.png

Code

coordinator = new WorkflowCoordinator( {workflow:'Workflow1'} );

var gr =   new GlideRecord("sys_group");

gr.addQuery("name", current.variables.group_name);

gr.addQuery("manager", current.variables.manager);

gr.query();

while(gr.next()) {

coordinator.add( {groupId: gr.sys_id+ ''} );

}

coordinator.add( {u_application_name : current.variables.v_application},   'Workflow2' );

coordinator;                 // THIS MUST BE THE LAST LINE

In the above script, I add the workflows to the coordinator object to trigger the subflows.

All the variables in the catalog item are available to the sub flow as workflow.inputs.variable_name.

Although the current object is available in the subflows, current.variables is not available. So you would have to use workflow.inputs to get the variables.   If you want to create a task within your subflows and you plan to use the same subflows again from the parallel flow launcher it is important to have the task creation within a Run Script activity. In fact, i would suggest to create Task using Run Script to make it more versatile.

Once your subflows are complete, you can have a return value activity in your sub flow to process the output in the Variables Parallel Flow Launcher script. In mycase I pass either true or false from my subflows to check if it was successfully completed or not. I check for the flow outputs in the parallel flow launcher of parent workflow and take further actions based on it. I can a success or a failure to a scratchpad variable that can be used to regulate further flow in the parent workflow.

find_real_file.png

var flowOutput = [];

var flowCount = coordinator.getNumFlows();

if(flowCount) {

  for(var index= 0; index < flowCount; index++) {

  flowOutput.push(coordinator.getFlow(index).output);

  }

}

if(flowOutput.indexOf(false) == -1) {

  workflow.scratchpad.returnProcessedValue = "Success";

} else {

  workflow.scratchpad.returnProcessedValue = "Failure";

}

I found this workflow activity to be pretty useful and since we all use catalog items a lot, leveraging this feature would be beneficial.

Comments
leahp
Kilo Guru

do you have an example of how to set up an array as the input?  



I have a requirement to run the same workflow "n" times based on the number of unique values in the array but I cannot figure out how to define that array in the input.   Any help or guidance would be great


venkatiyer1
Giga Guru

Hi Leah,



Sure, you can do it two ways if it is just one workflow you are planning to launch multiple times.



First, without advanced option checked choose the workflow   under workflow selection and in the inputs you can define an array of input values you are passing to each workflow.


[{u_server_name : 'Webapp1'   }, {u_server_name : 'Webapp2' }]



In the workflow you selected, you can retrieve the above sent value by defining an input variable with the name as u_server_name. And for each above object you will have a new workflow getting generated.



If you dont know how many objects of the array you would required and it needs to be done dynamically, you can choose the advanced option.



Click advanced option and within the script write the below script:-



Workflow1 is the name of the workflow you want to run multiple times.


Here below i have shown using a glide query but you can use any logic to ensure you keep adding an object to the coordinator variable and finally pass the coordinator variable for the workflow to pick up and run that many times.



coordinator = new WorkflowCoordinator( {workflow:'Workflow1'} );  


var gr =   new GlideRecord("sys_group");  


gr.addQuery("name", current.variables.group_name);  


gr.addQuery("manager", current.variables.manager);  


gr.query();  


while(gr.next()) {  


coordinator.add( {groupId: gr.sys_id+ ''} );  


}  



coordinator;  



Hope the above explanation helps. My above blog should illustrate it further.


leahp
Kilo Guru

Thanks, Venkat!



Is there a way for the input array to be defined something other than a variable?   We have a related list that shows servers to be build, and I want to query that related list and gather an array containing unique owning groups listed on the related list and use that as the input to create the tasks.



Does that make sense?   Is that possible?


venkatiyer1
Giga Guru

Yes that is possible. Is that related list a seperate m2m table or just a relationship that is connected via a reference? Either wise if you are able to query for the related list or the table   you should be able to use the advanced option to do it. say it is a m2m table from where you want to gather an unique owning groups you can do it first and then create the coordinator object as well.



var arr = [];


var gr =   new GlideRecord("the m2m table");


gr.addQuery("name", current.variables.group_name);


gr.addQuery("manager", current.variables.manager);


gr.query();


while(gr.next()) {


// you can write the logic here to ensure it is unique


  arr.push({groupId: gr.sys_id+ ''})


}


coordinator = new WorkflowCoordinator( {workflow:'Workflow1'} );


for(var index = 0; index < arr.length; index++) {


coordinator.add( arr[index] );


}


coordinator;


leahp
Kilo Guru

That worked perfectly to gather the array and it is looping through with various inputs but for some reason it doesn't spawn tasks for each group passed in the input.  


venkatiyer1
Giga Guru

Hi Leah,



Have you defined it as the input in the sub workflow you are calling? Also you can access that value within your sub flow using workflow.inputs.variable_name


Pravallika2
Kilo Contributor

Hi

I have one doubt, where did you declare groupId or u_application_name variable. in subflow?

venkatiyer1
Giga Guru

Workflow inputs

purbalipc
Tera Expert

Hi

where do you define workflow inputs. I don't see option to define input as listed link

 

Thanks

Purbali

Mollie V
Tera Guru

This is helpful. Do you know how to pass multiple input variables in the array? The workflow I want to call requires two input variables and I want to call this workflow multiple times using the parallel flow launcher.

Is this possible?

 

Thanks,

Mollie

 

Version history
Last update:
‎10-05-2017 10:18 AM
Updated by: