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
01-03-2023 01:48 AM
Hi @Community Alums
When you try to create new record or update existing then that type field must be on form visible. See the snapshot you have shared the type field is not present there and because of that you are getting value as undefined. In order to get the value you must have type field visible on 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 08:59 AM
Hi Gunjan,
I have made the suggested changes, now the 'type' field is visible on form always but the still the value is coming as 'undefined'.
Thanks,
J
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 07:17 AM
What you get here is expected: when the asynchronous from of getReference is used, it returns nothing - hence the undefined values.
Also because the asynchronous for of getReference is used there is an entire strategy that needs to be implemented to make it work; it as all described (and a solution is provided) in an article on SN Pro Tips.
But this should not be the solution that you should implement in your situation, as explained in other replies.
The reason why the script provided will not work, is because of its asynchronous nature. Thus different parts of that script will be executes in two steps.
First just this part will be executed:
function onSubmit () {
var type = g_form.getReference('type', doAlert);
alert("type is " + type);
}
Then the browser will go the Ajax call getReference defines. Once the browser completes the transaction it would execute the 2nd part:
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;
}
}
I say would, because in this case, where we are dealing with a submit situation, the 2nd part will not actually be executed, because by the time the Ajax call would complete and the result would arrive back, the page will have been submitted already.
But I am detailing this just FYI, you should rely on UI Policies (backed up server side/enforced by Data Policies) to get the result you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 10:36 AM - edited 01-04-2023 10:23 AM
Hello RJ8,
please revert your code back to the original code that you have in your first post.
Then change the condition from OR to AND by replacing || by && and also check for if parent field is empty, else it won't let the user allow to submit allthough parent field has been populated.
And as Gunjan said, remove the quotes from 'true'.
// use && (AND) instead of || (OR) and also check for if the parent field is empty
if ((type != 'Site') && (type != 'Region') && g_form.getValue('parent') == '') {
g_form.setMandatory('parent', true); // no quotes on true
Another thing that comes to mind is, that you should check if 'Site' and 'Region' are the actual technical names.
If not and if you don't use SNUtils yet, then check the choices of the dictionary entry by right-clicking on the field label and select 'Show choice list'. This will redirect you to a list of the available choices. There you check the value in the actual 'Value' column that relates to the labels 'Region' and 'Site'. I assume the technical values are 'site' and 'region'. But please check.
Example for the field 'Contact type':
Please let me know if that helped by marking it as solution if it worked, else leave a note with the steps done and with the output.
Best wishes 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2023 07:42 PM
If you do not expect the type to change between load and onSubmit, you can simply use a UI Policy. UI Policies will load and cache even field values that are not on the form, if those fields are port of the UI Policy conditions.