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

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Dileep2 

 

Can you share your Code for more insight?

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


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/

Hi @Ravi Gaurav ,

 

Thanks for your reply,

 

var wflow = new global.Workflow().cancel(current);
new global.Workflow().startFlow(gs.getProperty('x_ntc_plant_trial.plant_trial_main_workflow'), current);

 

Please see the attched script and system property. In the system property the value shows the sys id of Main workflow.

Thank you 

Hi @Dileep2 

You need to identify the context (instance) of the specific sub-workflow that you want to restart. The wf_context table stores the workflow contexts, which represent the active or completed instances of workflows.

And You should cancel the specific sub-workflow context that is stuck. This can be done using the cancelContext() method.

And Let Try the below script please:-

 

// Cancel the stuck sub-workflow
var wf = new Workflow();
var subWfContext = new GlideRecord('wf_context');
subWfContext.addQuery('parent', current.sys_id); // Assuming current is the record triggering the workflow
subWfContext.addQuery('workflow_version', 'sub_workflow_sys_id_here'); // Replace with the sub-workflow sys_id
subWfContext.addQuery('state', 'executing'); // Or the state that indicates the workflow is stuck
subWfContext.query();

if (subWfContext.next()) {
wf.cancelContext(subWfContext);
}

// Restart the specific sub-workflow
wf.startFlow('sub_workflow_sys_id_here', 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/

Hi @Ravi Gaurav ,

 

Thanks for your reply.

 

Have tried below script and it's not working. Can you please check once

Thank you

var terminated = 'terminated';
var subFlows = ['4688df971b699090cfec0dc2cd4bcb5a'];
// var subFlowsRestartList = [];
// var plantTrial = 'deab6f911bd01610deb5b8c2cc4bcb27';
var subFlowContexts = [];

// workflow contexts
var wfContext = new GlideRecord('wf_context');
wfContext.addEncodedQuery('state!=finished^ORstate=^id=' + current.getUniqueValue() + '^workflow.sys_idIN' + subFlows.join(','));
wfContext.query();
while (wfContext.next()) {
	subFlowContexts.push(wfContext.getUniqueValue());
}

// workflow logs
var subFlowLog = new global.PlantTrialGlobalUtils().wfLog(subFlowContexts, terminated);
var subFlowsRestartList = subFlowLog.split(',');

if (subFlowsRestartList.length > 0) {
	subFlowsRestartList.map(function (item) {
		// var wflow = new global.Workflow().cancel(current);
		new global.Workflow().startFlow(item, current);
	});
}