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

How to Check Multiple Mandatory fields in UI action

RudhraKAM
Tera Guru

I have a use case in UI action if the fields are empty we need to abort the action and display mandatory message  ,, we already has UI policies for making mandatory for those fields , but when i click on UI action it is ignoring the mandatory conditions 

 

function close(){



var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
var gDialog = new dialogClass('close2_record');
gDialog.setTitle('Publish ' + g_form.getValue('display_name') + ' to record Catalog');
gDialog.setPreference('sysparm_sys_id', g_form.getValue());
}

1 ACCEPTED SOLUTION

Try this ... I tested it out in a DEV instance.  It will print the Error Message just like your UI Policy would.

function close(){

var arr = g_form.getMissingFields();
var labels = [];

if (arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
labels.push(g_form.getLabelOf(arr[i]));
}
g_form.addErrorMessage('The following mandatory fields are not filled in: '+labels.toString());
return;
}

var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
var gDialog = new dialogClass('close2_record');
gDialog.setTitle('Publish ' + g_form.getValue('display_name') + ' to record Catalog');
gDialog.setPreference('sysparm_sys_id', g_form.getValue());
}

View solution in original post

9 REPLIES 9

Allen Andreas
Administrator
Administrator

Hi,

Please see this article for mandatory field check:

https://community.servicenow.com/community?id=community_blog&sys_id=a95d6629dbd0dbc01dcaf3231f961982

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen 

 

by adding this line to my code seems working to  display the alert message 

But how to abort the action if the alert is popup /mandatory fields are not filled 

 

function close(){

var arr = g_form.getMissingFields();
alert("The ID s of fields that are mandatory are not filled : " + arr);

var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
var gDialog = new dialogClass('close2_record');
gDialog.setTitle('Publish ' + g_form.getValue('display_name') + ' to record Catalog');
gDialog.setPreference('sysparm_sys_id', g_form.getValue());
}

 

 

function close(){

var arr = g_form.getMissingFields();
 
if (arr.length < 0) {
alert("The ID s of fields that are mandatory are not filled : " + arr);
return;
}



var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
var gDialog = new dialogClass('close2_record');
gDialog.setTitle('Publish ' + g_form.getValue('display_name') + ' to record Catalog');
gDialog.setPreference('sysparm_sys_id', g_form.getValue());
}

This will cancel out of the UI Action if there is nothing in the arr variable, but it will not reset whatever values the user has already put into the form.

Its still not working 😞