
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 07:15 PM
Hello Experts,
As a urgent requirement, I want the form focus to be on the mandatory fields which are left empty on Save/Submit.
Lets's say field A, B, C are on the 3rd tab in the form section of a form.
And I came to the 1st tab to fill in X and Y fields. I forget to fill in A, B, C and try to save.
Of course A, B, C being mandatory would be prompted to be filled in, but how do I set my form focus on those fields?
Code: UI Script - Condition if true:
===============================
function onCondition() {
var arr = g_form.getMissingFields();
alert(arr);
var ele;
setTimeout(setFocus(),0); //tried calling function as (setFocus,0) as well.
function setFocus()
{
for(var i=0; i<arr.length; i++)
ele = document.getElementById(arr[i]);
ele.focus();
}
}
Please help, urgent!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2023 09:07 AM
If you would like to avoid your end users to keep finding those missed mandatory fields on a form during submission, try this CS:
function onSubmit() {
var mandatoryFields = g_form.getMandatoryFields();
for (var i = 0; i < mandatoryFields.length; i++) {
var fieldName = mandatoryFields[i];
if (g_form.getValue(fieldName) == '') {
g_form.addErrorMessage(fieldName, 'This field is required.');
g_form.showFieldMsg(fieldName, true);
if (i == 0) {
g_form.setFocusOnField(fieldName);
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 08:25 PM
Hello,
You may find the below thread helpful.
https://community.servicenow.com/community?id=community_question&sys_id=708ff2e9db58dbc01dcaf3231f961900
Thanks,
Pradeep Sharma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2018 08:43 PM
Thanks I had checked this. But the only thing that is not working for me is that focus() does not navigate within the form section if there are mandatory fields empty within the form section tabs.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2023 09:07 AM
If you would like to avoid your end users to keep finding those missed mandatory fields on a form during submission, try this CS:
function onSubmit() {
var mandatoryFields = g_form.getMandatoryFields();
for (var i = 0; i < mandatoryFields.length; i++) {
var fieldName = mandatoryFields[i];
if (g_form.getValue(fieldName) == '') {
g_form.addErrorMessage(fieldName, 'This field is required.');
g_form.showFieldMsg(fieldName, true);
if (i == 0) {
g_form.setFocusOnField(fieldName);
}
}
}
}