Cancel workflow with UI Action

palmen
Tera Guru

I've got a requirement to be able to cancel a workflow at any given time during the time the workflow is active.

I found this thread which gave me more or less all answers on how to do this.

UI Action Cancel Workflow

What I need help with is that all open tasks in the workflow should be closed with a specific close note, but the ticket shouldn't get the same close note (it should still be blank).

My current code is as follows

function cancel(){  

    if(confirm('Are you sure you want to cancel the New Supplier process?')){  

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

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

        return true;   //Abort submission  

    }  

    return false;  

}  

 

 

// Server side code  

if(typeof window == 'undefined')  

    cancelNewSupplier();  

 

 

function cancelNewSupplier(){  

  current.active=false;  

  current.state=4;   //Closed Incomplete

  new Workflow().cancel(current);

  action.setRedirectURL(current);  

  current.update();  

  gs.addInfoMessage('The New Supplier Process has been canceled.');  

}

The function cancelNewSupplier() set the same state to all open tasks and the ticket which is OK, but the close code should only go to the open tasks.

How do I achieve this?

I tried with the following code but it only set the close notes for the ticket

current.close_notes='New Supplier Process were canceled by SCM';
1 ACCEPTED SOLUTION

This works fine, it set everything to the open tasks and no more tasks are being created.


But there is another thing that happened now, or should I say not happen. I do have a script that copy close notes from the task to the ticket and that script isn't executing anymore for the tasks being closed.



I guess I'll just have to make a decision which code to use, either the one you suggested (pasted below) or the one I used before which wasn't optimal (from my post Sep 3, 2015 2:33 PM)



function cancel(){


  if(confirm('Are you sure you want to cancel the New Supplier process?')){


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


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


  return true;   //Abort submission


  }


  return false;


}



// Server side code


if(typeof window == 'undefined')


  cancelNewSupplier();



function cancelNewSupplier(){


  //Close all active supplier tasks, set work notes and cancel the workflow.


  var gr = new GlideRecord('u_supplier_task');


  gr.addQuery('parent', current.sys_id);


  gr.addEncodedQuery('state!=3^state!=4'); //NOT Closed Complete or Closed Incomplete


  gr.addQuery();


  gr.query();


  while (gr.next()){


  gr.close_notes='Arbetsorder har automatiskt stängts när SCM avbrutit flödet för Ny Leverantör';


  gr.state = 4; //Closed Incomplete


  gr.setWorkflow(false);


  gr.update();


  }



  //Cancel workflow


  new Workflow().cancel(current);



  current.active=false;


  current.state=-7;   //Review


  current.close_notes='Flödet för Ny Leverantör har avbrutits av SCM, alla icke utförda arbetsorder har automatiskt stängts';


  current.update();


  action.setRedirectURL(current);


  gs.addInfoMessage('The New Supplier Process has been canceled.');


}


View solution in original post

17 REPLIES 17

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Fredrik,



You can GlideRecord to the change_task table and then only filter out with the state open and update the close notes.


rgm276
Mega Guru

Pradeep is correct, for example; here is an excerpt from a similar UI Action script I did recently


you might also consider a similar script to see if there are any user or group approvals that did not get closed



//=========================================================================================


//           Cancel any non completed change tasks after we cancel the change task


//=========================================================================================


              var gr1 = new GlideRecord('change_task');


              gr1.addEncodedQuery('change_request='+ current.sys_id+'^state!=3');     // ignore any that are already completed


              gr1.query();


              while(gr1.next()) {


                  gr1.state = 7;       // set state to closed skipped


                  gr1.update()


              }


  gs.addInfoMessage(gr1.getRowCount() + ' Change Tasks have been cancelled');     // notify users if we cancelled tasks


palmen
Tera Guru

I've almost got it working now but have some issues with the cancelation of the workflow.


This is my current code



function cancel(){


  if(confirm('Are you sure you want to cancel the New Supplier process?')){


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


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


  return true;   //Abort submission


  }


  return false;


}



// Server side code


if(typeof window == 'undefined')


  cancelNewSupplier();




function cancelNewSupplier(){


  current.active=false;


  current.state=-7;   //Review


  current.close_notes='Flödet för Ny Leverantör har avbrutits av SCM, alla icke utförda arbetsorder har automatiskt stängts';


  new Workflow().cancel(current);



  //Close all active supplier tasks.


  var gr = new GlideRecord('u_supplier_task');


  gr.addQuery('parent', current.sys_id);


  gr.addEncodedQuery('state!=3^state!=4'); //NOT Closed Complete or Closed Incomplete


  gr.addQuery();


  gr.query();


  while (gr.next()){


  gr.state = 4; //Closed Incomplete


  gr.close_notes='Arbetsorder har automatiskt stängts när SCM avbrutit flödet för Ny Leverantör';


  gr.update();


  }



  action.setRedirectURL(current);


  current.update();


  gs.addInfoMessage('The New Supplier Process has been canceled.');


}



The problem is row 19, when the workflow is canceled before I query all the supplier tasks.


The state is set correctly to Close Incomplete, but the close note will be blank for some reason (row 28 and 29). I still want the close notes to be set for all active supplier tasks.



If I cancel the workflow after I query all the tasks (insert row 19 to row 32), the workflow will keep on running and creating the remaining supplier tasks in the workflow that hadn't been created yet. This is not a behaviour I want.



I just want all active supplier tasks to be closed and no new supplier tasks should be created in the workflow and the close note should be set.


How can I do this?


Pradeep Sharma


Reverse these lines and check



  1. action.setRedirectURL(current);  
  2.   current.update();  


With



  1.   current.update();  
  2. action.setRedirectURL(current);