- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 08:31 AM
Hi
I have a UI Action in change with a script that checks mandatory fields. But it only checks one fields at the times.
How do i set the UI Action to check for all fields onClick.
function approval(){
if(g_form.getValue('short_description') == ''){
g_form.hideFieldMsg('short_description');
g_form.setMandatory('short_description', true);
g_form.showFieldMsg('short_description','short description are mandatory.','error');
return false;
}
if(g_form.getValue('description') == ''){
g_form.hideFieldMsg('description');
g_form.setMandatory('description', true);
g_form.showFieldMsg('description','Description are mandatory.','error');
return false;
}
if(g_form.getValue('change_plan') == ''){
g_form.hideFieldMsg('change_plan');
g_form.setMandatory('change_plan', true);
g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');
return false;
}
if(g_form.getValue('backout_plan') == ''){
g_form.hideFieldMsg('backout_plan');
g_form.setMandatory('backout_plan', true);
g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');
return false;
}
if(g_form.getValue('test_plan') == ''){
g_form.hideFieldMsg('test_plan');
g_form.setMandatory('test_plan', true);
g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');
return false;
}
gsftSubmit(null, g_form.getFormElement(), 'request.approval');
}
action.setRedirectURL(current);
current.approval='requested';
current.state=9;
current.update();
I am not a scripting expert, so keep it simple. 🙂
Thomas
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 08:45 AM
Try this
function approval(){
var return_flag=true;
if(g_form.getValue('short_description') == ''){
g_form.hideFieldMsg('short_description');
g_form.setMandatory('short_description', true);
g_form.showFieldMsg('short_description','short description are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('description') == ''){
g_form.hideFieldMsg('description');
g_form.setMandatory('description', true);
g_form.showFieldMsg('description','Description are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('change_plan') == ''){
g_form.hideFieldMsg('change_plan');
g_form.setMandatory('change_plan', true);
g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('backout_plan') == ''){
g_form.hideFieldMsg('backout_plan');
g_form.setMandatory('backout_plan', true);
g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('test_plan') == ''){
g_form.hideFieldMsg('test_plan');
g_form.setMandatory('test_plan', true);
g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');
return_flag=false;
}
if(!return_flag)
return return_flag;
gsftSubmit(null, g_form.getFormElement(), 'request.approval');
}
action.setRedirectURL(current);
current.approval='requested';
current.state=9;
current.update();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 08:36 AM
Hi Thomas,
You've got a mix of client side and server side script in this UI action.
I recommend using a UI policy or client script to make your fields mandatory based on your conditions. The UI action will automatically enforce the mandatory fields.
Creating a UI Policy - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 08:40 AM
Hi Tomas,
try something like:
- function approval(){
- var count = 0;
- if(g_form.getValue('short_description') == ''){
- g_form.hideFieldMsg('short_description');
- g_form.setMandatory('short_description', true);
- g_form.showFieldMsg('short_description','short description are mandatory.','error');
- count = 1;
- }
- if(g_form.getValue('description') == ''){
- g_form.hideFieldMsg('description');
- g_form.setMandatory('description', true);
- g_form.showFieldMsg('description','Description are mandatory.','error');
- count = 1;
- }
- if(g_form.getValue('change_plan') == ''){
- g_form.hideFieldMsg('change_plan');
- g_form.setMandatory('change_plan', true);
- g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');
- count = 1;
- }
- if(g_form.getValue('backout_plan') == ''){
- g_form.hideFieldMsg('backout_plan');
- g_form.setMandatory('backout_plan', true);
- g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');
- count = 1;
- }
- if(g_form.getValue('test_plan') == ''){
- g_form.hideFieldMsg('test_plan');
- g_form.setMandatory('test_plan', true);
- g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');
- count = 1;
- }
- if(count == 1)
- {
- return false;
- }
- gsftSubmit(null, g_form.getFormElement(), 'request.approval');
- }
- action.setRedirectURL(current);
- current.approval='requested';
- current.state=9;
- current.update();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 08:45 AM
Try this
function approval(){
var return_flag=true;
if(g_form.getValue('short_description') == ''){
g_form.hideFieldMsg('short_description');
g_form.setMandatory('short_description', true);
g_form.showFieldMsg('short_description','short description are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('description') == ''){
g_form.hideFieldMsg('description');
g_form.setMandatory('description', true);
g_form.showFieldMsg('description','Description are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('change_plan') == ''){
g_form.hideFieldMsg('change_plan');
g_form.setMandatory('change_plan', true);
g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('backout_plan') == ''){
g_form.hideFieldMsg('backout_plan');
g_form.setMandatory('backout_plan', true);
g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');
return_flag=false;
}
if(g_form.getValue('test_plan') == ''){
g_form.hideFieldMsg('test_plan');
g_form.setMandatory('test_plan', true);
g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');
return_flag=false;
}
if(!return_flag)
return return_flag;
gsftSubmit(null, g_form.getFormElement(), 'request.approval');
}
action.setRedirectURL(current);
current.approval='requested';
current.state=9;
current.update();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 09:30 AM
Thanks That works.