Cancel Request UI Button

Anna L
Tera Contributor

Is it viable to have Cancel Request UI Button on Task form and Request Item form and once the user clicks the button, a message should popup as a warning. How would you do it? Any suggestions?

1 ACCEPTED SOLUTION

You will have to add a client side code to UI action. I have just added the popup message to the OOB UI action 'Cancel request'


Here is the script for that


function cancelRequest(){


  var submitFlag= confirm("If you cancel request, workflow will be stopped");


  if(!submitFlag){


  return false;   //Abort submission


  }


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


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


}


//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


  runBusRuleCode();




//Server-side function


function runBusRuleCode(){



  current.request_state = 'closed_cancelled';


  current.update();


  new UIActionUtils().approvalsNoLongerRequired(current.sys_id);


}



ui.PNGUI1.PNG


This link will be helpful on how to write both client side and server side code in a single UI action


Client & Server Code in One UI Action - ServiceNow Guru



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

12 REPLIES 12

You will have to add a client side code to UI action. I have just added the popup message to the OOB UI action 'Cancel request'


Here is the script for that


function cancelRequest(){


  var submitFlag= confirm("If you cancel request, workflow will be stopped");


  if(!submitFlag){


  return false;   //Abort submission


  }


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


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


}


//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


  runBusRuleCode();




//Server-side function


function runBusRuleCode(){



  current.request_state = 'closed_cancelled';


  current.update();


  new UIActionUtils().approvalsNoLongerRequired(current.sys_id);


}



ui.PNGUI1.PNG


This link will be helpful on how to write both client side and server side code in a single UI action


Client & Server Code in One UI Action - ServiceNow Guru



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Hey Abhinay,



This works seamlessly, but on request form. What if user can see the same UI button on task itself, do you see that being possible and good practice use case?


If you want that UI action on the catalog task or request item, you will have to duplicate the UI action and change the table to sc_task and sc_req_item instead and you need to slightly tweak your code in the UI action



Instead of current.request_state = 'closed_cancelled'; add this in UI action current.request.request_state='closed_cancelled';



Don't forget to mark it as correct.



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Hello Abhinay,

Have you done this within a Service Portal catalog widget?  So from the Service Portal, the user has a 'Cancel' button on the widget that cancels the request.  Excuse me if there is a widget to build off from but I have seen any OOB Widgets that have this functionality or something like it.

Thank you,

-Wesley

 

 

I'm new to scripting in SN as well and wondering where this functionality is at??  Is there supposed to be a form button on the Request itself??  in SP?