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

Michael Fry1
Kilo Patron

This article shows you how to write the UI action as you want to use a mixture of client-side and server-side code: Client & Server Code in One UI Action - ServiceNow Guru


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);


}


shamma
Kilo Expert

Hi Brian





/Client-side 'onclick' function


function reopen(){


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


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


          g_form.hideFieldMsg('comments');


          g_form.setMandatory('comments', true);


          g_form.showFieldMsg('comments','Comments are mandatory when reopening an Incident.','error');


        return false; //Abort submission


    }


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


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


    serverReopen();



function serverReopen(){


    //Set the 'State' to 'Active', update and reload the record


    current.state = 2;


    current.update();


    gs.addInfoMessage('Incident ' + current.number + ' reopened.');


    action.setRedirectURL(current);


}



See if that works!!



Thanks.


Shamma


Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi Brian,



As per my understanding you want comments mandatory when reject button is clicked.



so for this purpose you need to write a onSubmit Client script on Sys_approval table as below:



function onSubmit() {



  var action = g_form.getActionName();



  if (action == 'UI Action Name') {/put your UI Action Name here.



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


  {


  g_form.setDisplay('u_need_info',true);


  g_form.setMandatory('u_need_info',true);


  g_form.setMandatory('comments',true);


  g_form.showFieldMsg('comments', 'Please Fill In Comments', 'error',true);


  return false;


  }


  }


}





After doing this: (Note: UI Action must be client callable so tick client as true)


in Your UI Action write:


//do what you want here...then put below two lines.


current.update();


action.setRedirectURL("Where you want to redirect after submission");//current or previous.



this will solve your problem.



Thank You,


HIt correct or helpfull.