The CreatorCon Call for Content is officially open! Get started here.

Restart Subflow in Main Workflow with UI Action

Dileep2
Tera Contributor

Hi All,

 

I have 20 sub workflows created for custom application and these workflows called from the Main workflow. With the help of UI Action "Restart Workflow" how to restart the particular sub flow instead of starting the entire workflow.

 

have created UI Action "Restart Workflow" but it is triggering all the sub workflow approvals but I need only to restart the Sub flow which is stuck.

Please suggest.

 

Thank you

11 REPLIES 11

Dileep2
Tera Contributor

Please see the Script Include called in the above script - PlantTrialGlobalUtils

	wfLog: function (subFlowContexts, terminated) {
		var subFlowsRestartList = [];
		var wfLog = new GlideRecord('wf_log');
		wfLog.addEncodedQuery('context.sys_idIN' + subFlowContexts.join(',') + '^messageLIKE' + terminated);
		wfLog.query();
		while (wfLog.next()) {
			subFlowsRestartList.push(wfLog.context.workflow.toString());
		}

		return subFlowsRestartList.join(',');
	},

 

Hi @Dileep2 

can you check the below Script :-

 

(function() {
// Termination state
var terminated = 'terminated';
// List of sub-flows to be checked
var subFlows = ['4688df971b699090cfec0dc2cd4bcb5a'];
var subFlowContexts = [];

// Query to fetch the workflow context for the current record and specific sub-flow(s)
var wfContext = new GlideRecord('wf_context');
wfContext.addEncodedQuery('state!=finished^ORstate=^id=' + current.getUniqueValue() + '^workflow.sys_idIN' + subFlows.join(','));
wfContext.query();

// Collect sub-flow contexts
while (wfContext.next()) {
subFlowContexts.push(wfContext.getUniqueValue());
}

// Assuming global.PlantTrialGlobalUtils().wfLog returns a comma-separated string of sub-flow context IDs
var subFlowLog = new global.PlantTrialGlobalUtils().wfLog(subFlowContexts, terminated);
var subFlowsRestartList = subFlowLog.split(',');

// Iterate through each stuck sub-flow context and restart it
if (subFlowsRestartList.length > 0) {
subFlowsRestartList.forEach(function(subFlowContextId) {
// Cancel the sub-flow
var subFlowContext = new GlideRecord('wf_context');
if (subFlowContext.get(subFlowContextId)) {
// Cancel the sub-flow context
new global.Workflow().cancel(subFlowContext);

// Restart the sub-flow context
new global.Workflow().startFlow(subFlowContext.workflow, current);
}
});
}
})();

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

OlaN
Giga Sage
Giga Sage

Hi,

Why is the subflow stuck in the first place?

Maybe you should investigate that issue?

Instead of building a custom solution on top of something that isn't working..

Dileep2
Tera Contributor

Hi @OlaN

 

Thanks for your reply!

 

We are working with ServiceNow team for the issue with stuck sub workflow and still they are checking on the issue. As this issue is happening frequently and to overcome the issues and we have decided to create an UI Action.. 

Users are facing lot of issues because of that we decided to create the ui action.

Mark Manders
Mega Patron

For starting a subflow you also need to send the input for the subflow from the ui action.

But the fact that your subflow is stuck and it can't be resolved easily, it may be a good thing to go back to the drawing board. It doesn't sound like it's all setup in a way that will give your users confidence in the process.

Starting it again, may also get it stuck again.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark