How to make a field mandatory on submit based on the field not visible on form on submit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2022 07:48 AM
How to make a field mandatory on submit based on the field not visible on form on submit but gets auto populated based on selections made through a different client script
We are trying to set Parent field mandatory based on the value on 'Type' field, but the 'Type' field is hidden on submit. Based on the below links selected the value for 'Type' field will be populated
I tried onSubmit client script, but it did not work. Parent field is becoming mandatory for all types. Any help is highly appreciated.
var type = g_form.getValue('type');
if ((type != 'Site') || (type != 'Region')) {
g_form.setMandatory('parent', 'true');
var msg = getMessage("Parent field is mandatory, please fill and submit.");
g_form.showFieldMsg("parent", msg, "error");
return false;
}
Thanks,
J
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2022 07:52 AM
Hi @Community Alums
Make the changes as below :-
g_form.setMandatory('parent', true);
Don't write true in single quote. Also check the backend value of Site and Region type.
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 12:59 AM
Hi Gunjan,
Thank you for the response.
I made the changes as below, it seems no value coming into the 'type'. Let me know if you have any suggestions. Is it because we are checking this onSubmit and system will not know the type until the record is submitted?
function onSubmit() {
var type = g_form.getReference('type', doAlert);
alert("type is " + type);
}
function doAlert(type) {
if ((type != 'Site') || (type != 'Region')) {
g_form.setMandatory('parent', true);
var msg = getMessage("Parent field is mandatory, please fill and submit.");
g_form.showFieldMsg("parent", msg, "error");
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 01:10 AM
Hi @Community Alums ,
You are not getting the value of type field, Below are the possible reasons. Please explore and let me know:-
- There might be a typo in the field name of 'type'. Make sure the field name is spelled correctly in the
getReference
function. - The
getReference
function might not be able to find the field 'type'. Make sure the field actually present in the form.
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 01:44 AM
Hi Gunjan,
Type field is present on my location table and below is the info, please check
Thanks
J