The CreatorCon Call for Content is officially open! Get started here.

UI action mandatory field

Brian Lancaster
Tera Sage

We recently noticed that when a user rejects a request using the UI Action of Reject it does not force them to enter comments.   I'm trying to make it so comments are mandatory on reject even when using the UI button.   I put the following code in the UI action but it is sending me back to the previous screen and leaving the state of the approval as requested.   What am I missing so it does not redirect to the previous screen?

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

  g_form.setMandatory('comments', 'true');

  g_form.showFieldMsg('comments', 'Comments are required when rejecting an approval','info');

}

else{

  action.setRedirectURL('home.do');

  current.state='rejected';

  current.update();

}

1 ACCEPTED SOLUTION

LaurentChicoine
Tera Guru

The problem with your script is that it uses both client and server side script without doing the proper adjustments to do so.



g_form is a client side object while action and current are server side objects.



Also if you would like to use client side, you would need to check the Client checkbox and pass a function call in the Onclick field and define this function in the script. As said by Michael you could refer to the ServiceNow Guru article to make a UI action that uses both server and client side script: Client & Server Code in One UI Action - ServiceNow Guru



However, in your case all this can be handled with server script. As of Fuji here is what the default UI Action script looks like to avoid rejecting without leaving a comment:



current.state = 'rejected';


if(!JSUtil.nil(current.comments))


      current.update();


else{


      gs.addErrorMessage("Comments are required when rejecting an approval");


      current.state = 'requested';


      current.setAbortAction(true);


}


View solution in original post

5 REPLIES 5

Hi Ashutosh,




There is no need to write the client script for comments mandatory as UI action works on Client and Server Side both so you can write your both code in UI action.



See the code above I mentioned upper portion is for client side coding and lower portion is for Server Side.



Let ne know if you have any questions.



Regards,


Shamma