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-04-2023 11:34 AM - edited 01-05-2023 05:34 AM
Hello @Community Alums
regarding your On Submit Client Script approach based on the fact that have to handle a list field you can do the following:
function onSubmit() {
// get all the values (sysids) inside the list field in string format
// and change it into an array:
var aTypeList = g_form.getValue('type').split(',');
// get value of parent field:
var sParent = g_form.getValue('parent');
/* Check if indexOf() for one ot the values (technical value needed) is -1
** (means it's not part of the list field)
** AND (&&) check if parent is empty or null --> this is necessary, else:
** it would never allow the user to submit although the field has been populated
**
** Now if one of the two values is missing (*) and if parent field is empty
** the form submit will be prevented and the message will be shown
** (*) If only one value needs to be part of the list field in order to prevent the submit,
** then just change the || to an &&
*/
/* Since you listed sys_ids in your last replies for 'site' and 'region',
** the if condition would look like this:
*/
if ((aTypeList.indexOf('68038ff697454d10769b36b3f153afd5') == -1
|| aTypeList.indexOf('64f24f3a97454d10769b36b3f153af69') == -1)
&& (sParent == '' || sParent == null)) {
/* Else you might need to check for the actual technical values,
** that depends on the reference / configuration used for your type field,
** so the if condition could also look just like this:
*/
if ((aTypeList.indexOf('site') == -1 || aTypeList.indexOf('region') == -1)
&& (sParent == '' || sParent == null)) {
g_form.setMandatory('parent', true);
var msg = getMessage("Parent field is mandatory, please fill and submit.");
g_form.showFieldMsg("parent", msg, "error");
return false;
}
}
How to check for the actual values of site and region for your if-condition?
If you do not yet use SN Utils then in Background Script you can run something like this:
var gr = new GlideRecord('incident'); // insert ypur table_name
gr.get('e8caedcbc0a80164017df472f39eaed1'); // enter sys_id of a record where type field contains site and region
var sTypeList = gr.getValue('u_type'); // insert the technical name of your type field
gs.info(sTypeList); // This will return either two sys_id or two values such as 'site' and 'region'
Else with SNUtils installed ad your form open, you can press 'Ctrl+Shift+/' and write 'tn' into the inpit field and press enter.
This will show you the technical names next to the display names and could look like this:
--
Options and usability:
Regarding the reply from user -O- that making fields mandatory on submit is not very user friendly I totally agree, but this always depends on the reqirements needed. There will be several situations where the customer explicitly requests this behavior or if using a Data Policy you would have the same behavior on a form by configuration.
Speaking of Data Policies: Another thing that you have to keep in mind is that neither UI Policies nor Client Scripts apply on List edit.
This means, if you are on the incident list and you want to change some value there for a record that meets your conditions, then (if not restricted) this might be still possible for the user without populating the field that you actually want to be populated (in your case the parent field).
So for this use case you can think of using a Data Policy as well instead or in addition.
--
UI Policy condition builder:
Regarding your condition on the UI Policy it should look something like this:
So you choose your Type field (in my case it is a custom one) and select the value that it should NOT contain. Please make sure that you check for the correct values, this depends on your fields configuration / reference. Best is to use the magnifying glass icon in order to select the correct entries.
Note: You need to add an AND condition that checks for Parent "is empty". Else it won't work as expected.
or
--
Hope some of this helps and if you have further questions then don't hesitate to ask.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2023 05:05 PM
RJ8,
Type is a list field it is going to contain a comma separated list of sysIDs. Try using a UI Policy like this one.