Calling a sub workflow

superjames
Kilo Contributor

I would like to call a workflow from a workflow, but I need to set a different current record in the sub flow. I want the parent workflow to call the sub flow and wait for the return. I have been using the methodology from this: https://wiki.servicenow.com/index.php?title=Using_Subflows

This calls the sub flow and waits for completion, but does not set the current record in the sub flow, so I pass the record needed as input and use this code in the sub flow:

// Setup current record from input

    current = new GlideRecord('u_job_set');

    current.get(workflow.scratchpad.u_current_record);

    current.state = 1;

// Setup context

    context.table = 'u_job_set';

    context.id = current.sys_id;

    context.state = 'executing';

This works, but there are some timing issues, and I suspect there is more that needs to be done. I found this in the AWS workflows.

My other option is to have a run script that launches the workflow. This seems to work better in the sub flow, but does not wait for subflow completion:

var jobSet = new GlideRecord('u_job_set');

jobSet.get(workflow.scratchpad.cRecord);

var wf = new Workflow();

var vars = {};

var workflowId = wf.getWorkflowFromName('Sub Flow 1');

wf.startFlow(workflowId, jobSet, "", vars);


So, Is there a way to pause after the workflow launch here? I know I can do it with some business rules and database variables, but looking form something cleaner.

Or, is there a better way to set the current record in the first case? I am also using the parallel flow launcher and the WorkflowCoordinator object to launch subflows. These behave just like the first example.


J.

1 ACCEPTED SOLUTION

James,



What you can do is in your Parent Workflow after the Runscript which invokes the Workflow on Job table, have a Wait For Condition to wait for some flag like a custom field 'u_job_wait'.


Now, in the Workflow which is running on the Job at the end of the Workflow, update the record of the parent with u_job_wait to true which then will have the Workflow move forward.


View solution in original post

7 REPLIES 7

poyntzj
Kilo Sage

I use sub workflows and no timing issues that i can see


The sub workflow is called and the options are passed and once the sub workflow finishes it returns back to the parent workflow.


If it returns a value you can then heck and process.



On whatever it is your calling, if you use the show workflow button, does it show you the two workflows and the sub one completed or still in process ?


The sub flow is still in process after the "parent" flow has completed. Actually, the parent flow launches multiple of the same subflow then completes while the subflows are still running.


I would also look at copying your existing workflow and make it run just 1 copy of the subworkflow for now and check that runs as expected.



Are the same subflows running in parallel or serial ?


I found that if I ran in parallel I had to set the variables a little differently and even add a little delay


so for sub workflow 1 I will set out_variable; out_variable2


for sub workflow 2 I will set out2_variable, out2_variable2


and so on


the return values are all different



I also call a couple of complex routines and at one point I was getting a really odd issue.


In the end, I established that the functions were getting a bit skewed, even though the main function is anonymous.


So I let the let first sub workflow run, before the 2nd is called I add a 5 second delay, before the 3rd a 10 second delay and so on.


manikorada
ServiceNow Employee
ServiceNow Employee

James,



Why do you need the Subflow to have different current. You can have   a Runscript to invoke a Workflow in your parent Workflow and have these two Workflow dependent on each other