Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to restart a workflow from UI action

Yashaswini
Tera Expert

Hi,

Could you please tell me how to restart a workflow from UI action?

Have used below script:

UI Action

Table:sc_req_item

new Workflow().restartWorkflow(current, true);
current.state = '1';
current.update();
action.setRedirectURL(current);

 

This script restarts the workflow,but the issue is if there are 2 levels of approval and 1st level of approval is approved and 2nd is requested, when this UI action button is clicked 1st level of approval is still in approved state. I want to restart the approvals as well.

Kindly request you to suggest solution for this.

 

Regards,

Yashaswini

7 REPLIES 7

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Yashaswini,

Here you go.

  new WorkflowApprovalUtils().cancelAll(current, comment);
      new Workflow().restartWorkflow(current);
current.state = '1';
current.update();
action.setRedirectURL(current);

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

If the above doesn't work then try this

new Workflow().cancel(current);
new WorkflowApprovalUtils().cancelAll(current, comment);
new Workflow().restartWorkflow(current);
current.state = '1';
current.update();
action.setRedirectURL(current);

 

Thanks for replying.

Have tried both the codes. Workflow is restarted but approvals are not reset.

 

Regards,

Yashaswini

Try with below code.

 

var approvals = new GlideRecord('sysapproval_approver');
approvals.addEncodedQuery('^sysapproval=' + current.sys_id + '^ORdocument_id=' + current.sys_id);
approvals.query();
while(approvals.next()){
	approvals.state = 'not requested';  
	approvals.comments = 'This approval was manually reset';
	approvals.update();
}

//Which workflow are we working with?
var thisWorkFlow = '';
var whichWorkFlow = new GlideRecord('wf_context');
whichWorkFlow.addEncodedQuery('id='+current.sys_id);
whichWorkFlow.setLimit(1);
whichWorkFlow.query();

if(whichWorkFlow.next()){thisWorkFlow = whichWorkFlow.workflow_version.toString();}

//Now delete the current workflow
new Workflow().deleteWorkflow(current); 

//Reset this record back to not requested and a draft state
//Finally, restart the workflow
if(thisWorkFlow!=''){
	var wf = new Workflow();
	var context = wf.startFlow(thisWorkFlow, current, current.update());
}
current.approval = 'not requested';
current.state = '1';//Draft
current.update();
action.setRedirectURL(current);