How to make a field mandatory on submit based on the field not visible on form on submit

Community Alums
Not applicable

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

RJ8_0-1672415132741.png

RJ8_1-1672415187403.png

 

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

 

 

16 REPLIES 16

Gunjan Kiratkar
Kilo Patron
Kilo Patron

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

Community Alums
Not applicable

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;
}
}

 

RJ8_0-1672736282314.png

 

Hi @Community Alums ,

 

You are not getting the value of type field, Below are the possible reasons. Please explore and let me know:-

  1. There might be a typo in the field name of 'type'. Make sure the field name is spelled correctly in the getReference function.
  2. 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

Community Alums
Not applicable

Hi Gunjan,

 

Type field is present on my location table and below is the info, please check

RJ8_0-1672739018957.png

Thanks

J