Reset Workflow with a UI Action

dianemiro
Kilo Sage

Hi All,

Anyone who has an idea how to reset or restart a workflow with a UI Action? Basically, I have my UI Action made and here is my configuration:

find_real_file.png

With this script:

function back2Draft() {

  //Set the 'State' values to 'Review', and display mandatory fields

  g_form.setValue('state',1);

  //Call the UI Action and skip the 'onclick' function

  gsftSubmit(null, g_form.getFormElement(), 'demand.back2Draft'); //MUST call the 'Action name' set in this UI Action

}

if (typeof window == 'undefined')

  ServerBack2Draft();

function ServerBack2Draft() {

  var objctask = new GlideRecord("change_task");

  objctask.addQuery('u_demand',current.sys_id);

  objctask.query();

  while(objctask.next()){

  objctask.state = 7;

  objctask.update();

  new WorkflowApprovalUtils().cancelAll(current, comment);

  new Workflow().restartWorkflow(current,false);

  current.state = 1;

  current.update();

  action.setRedirectURL(current);

  }

}

The Demand State is not changing and the Change Tasks only Closed Skipped the 1st task.

find_real_file.png

Many thanks!

1 ACCEPTED SOLUTION

preddy
Kilo Guru

Hi Diane,



Can you see the below code I was updated and added the code to delete task.



cancelWorkflow();


deleteTasks();



current.state = 6;


gs.addInfoMessage('Change ' + current.number + ' has been reset. Any open tasks have been cancelled and the Change will need to go through the Approval process again, if required.');


current.update();



function cancelWorkflow(){


new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


var wf = new Workflow();


wf.deleteWorkflow(current);


}



function deleteTasks(){


// Delete any running change tasks


var cTask = new GlideRecord('change_task');


cTask.addQuery('change_request',current.sys_id);


cTask.query();


while (cTask.next()){      


      cTask.deleteRecord();      


}


}


}


Please let me know if my comment was helpful and mark it accordingly (like, helpful, correct) so that others can be helped.


View solution in original post

7 REPLIES 7

preddy
Kilo Guru

Hi Diane,



Create Ui action and give action name and paste this below code in your action and test this .




cancelWorkflow();



current.state = 6;


gs.addInfoMessage('Change ' + current.number + ' has been reset. Any open tasks have been cancelled and the Change will need to go through the Approval process again, if required.');


current.update();



function cancelWorkflow(){


new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


var wf = new Workflow();


wf.deleteWorkflow(current);


}




Please let me know if my comment was helpful and mark it accordingly (like, helpful, correct) so that others can be helped.


Hi Pavan,



Thank you so much for this code. Your code didn't remove the tasks related to my Demand. I added a few lines of code and it worked now! Many thanks for your help!!



cancelWorkflow();



  current.state = 1;


  gs.addInfoMessage('Demand ' + current.number + ' has been set to Draft. Any open tasks have been Closed Skipped and the Demand will need to go through the Approval process again, if required.');


  current.update();


  function cancelWorkflow(){


  new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


  var wf = new Workflow();


  wf.deleteWorkflow(current);


  }



  var objctask = new GlideRecord("change_task");


  objctask.addQuery('u_demand',current.sys_id);


  objctask.query();


  while(objctask.next()){


  objctask.state = 7;


  objctask.work_notes = "Demand was set to Draft, setting this CTASK to Closed Skipped";


  objctask.update();


  }


  action.setRedirectURL(current);


preddy
Kilo Guru

Hi Diane,



Can you see the below code I was updated and added the code to delete task.



cancelWorkflow();


deleteTasks();



current.state = 6;


gs.addInfoMessage('Change ' + current.number + ' has been reset. Any open tasks have been cancelled and the Change will need to go through the Approval process again, if required.');


current.update();



function cancelWorkflow(){


new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


var wf = new Workflow();


wf.deleteWorkflow(current);


}



function deleteTasks(){


// Delete any running change tasks


var cTask = new GlideRecord('change_task');


cTask.addQuery('change_request',current.sys_id);


cTask.query();


while (cTask.next()){      


      cTask.deleteRecord();      


}


}


}


Please let me know if my comment was helpful and mark it accordingly (like, helpful, correct) so that others can be helped.