How to make a Additional Comments field mandatory using UI policy?

Abdul Ahad
Kilo Contributor

I am new to Service Now. I have been assigned a task in which in the first step I need to create a UI action on an incident table and I have done it. The second step includes setting the additional comments mandatory on clicking the UI action and deliver a field message reading: "Please add additional information".

Kindly help me completing my SNOW task.

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Salam Abdul,

Your Question title and question description is contradicting. UI action and UI policy are two different things. So, I am going by question description and this is your code to make additional comments field mandatory and showing the field msg using UI action

Make sure client checkbox is "checked", action name is "mandatory_comments" & onClick is "reopen()"

//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.setMandatory('comments', true);
      g_form.showFieldMsg('comments','Please add additional Information.','error');
      return false;  //Abort submission

   }

   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), 'mandatory_comments'); //MUST call the 'Action name' set in this UI Action
}

 

Please mark this correct & helpful if it answered your question.

Thanks & Regards,
Sharjeel

Regards,
Muhammad

View solution in original post

12 REPLIES 12

MrMuhammad
Giga Sage

Salam Abdul,

Your Question title and question description is contradicting. UI action and UI policy are two different things. So, I am going by question description and this is your code to make additional comments field mandatory and showing the field msg using UI action

Make sure client checkbox is "checked", action name is "mandatory_comments" & onClick is "reopen()"

//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.setMandatory('comments', true);
      g_form.showFieldMsg('comments','Please add additional Information.','error');
      return false;  //Abort submission

   }

   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), 'mandatory_comments'); //MUST call the 'Action name' set in this UI Action
}

 

Please mark this correct & helpful if it answered your question.

Thanks & Regards,
Sharjeel

Regards,
Muhammad

Thank you so much. It worked successfully.

 

Glad to be of help 🙂

 

@Abdul Ahad It would be great if you mark my response as CORRECT so that others can see this on top of the list and get benefited by this. 

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Pooja Mallikarj
Kilo Sage

Hi,

You can do this with onSubmit client script please refer below code.

function onSubmit() {
   //Get the action value for the button clicked
   var action = g_form.getActionName();
 
   if(action == 'cancel') //put the action name from your UI action 

  {
      //Make 'comments' mandatory 
      g_form.setMandatory('comments',true);

      g_form.addErrorMessage('Please add additional information');

     return false;
   }
}

For more information you can refer the below link.
https://www.servicenowguru.com/scripting/client-scripts-scripting/identifying-ui-action-clicked-subm...

Thanks,

Pooja M