Make comments mandatory on click of a ui action

luke_james2
Kilo Explorer

Good Afternoon everyone,

 

I currently have an issue where I need to make the comments field on the sys_approval table mandatory when a user is rejecting a change. This would work by the user clicking on the reject ui action and then it would not let them reject the change until a comment has been put into the comments field. Has anybody done this before?

 

Thanks,

 

Luke

10 REPLIES 10

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I had a similar requirement and below is UI Action code that should work.   I based this off of the out of the box code for the Resolve Incident UI Action since it behaves in a similar manner.



  1. Personalize the Reject UI Action - the Form Button, not the list action
  2. Change the action name to "reject_approval" - this needs to be changed because action name needs to be unique for the code below and there are many out of the box UI Actions with an action name of "reject".
  3. Check the Client Checkbox
  4. Set the OnClick value to rejectApproval();
  5. Leave the condition as it is
  6. Replace the script with the code below.   Feel free to edit the error messages in the code.


function rejectApproval(){


      g_form.setValue('state', 'rejected');


     


      if (g_form.getValue('comments') == '') {


              //Remove any existing field message, set comments mandatory, and show a new field message


              try {g_form.hideFieldMsg('comments');} catch(e) {}


              g_form.setMandatory('comments', true);


              g_form.showFieldMsg('comments','Reason is required when rejecting a Change','error');


              return false;   //Abort submission


      }


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


      gsftSubmit(null, g_form.getFormElement(), 'reject_approval'); //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')


      serverReject();



function serverReject(){


      current.state = 'rejected';


      current.update();


}


Hi Everyone,



Thanks for your thoughts on this. I've tried Michael's method and in the main this works. The only issue is that it is not changing the state to rejected after comments are entered and the reject ui action button has been clicked. Any thoughts?


make sure that in the above code line#12


gsftSubmit(null, g_form.getFormElement(), 'reject_approval');




you call the correct action name, for Michael it maybe reject_approval, for you it might be "reject". Just take a look at the Action Name for your reject button and replace this with that


This works for me, I am on Eureka Patch 1 and have another instance on Patch 5.   Can you please provide exact step by step to recreate?


Brilliant. I've got it working now Harikrishan was right i hadn't changed the action name to the 'reject approval'. Now updated it and it works.



Many Thanks,



Luke