Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

I have created an onSubmit cat. Client Script to show an alert if a field is not filled by the user

deepakmaury
Tera Contributor

I have created an onSubmit  catalog Client Script to show an alert if a field is not filled by the user. However, when I make that field mandatory from the Variable settings, the alert does not appear. I also tried using a  catalog Client Script and a  catalog UI Policy, but the alert is still not working. What could be the reason, and how can I fix this?

7 REPLIES 7

lpruit2
Mega Sage

Greetings @deepakmaury. If you make the field mandatory, what is the value of also having an onSubmit Catalog Client Script? The mandatory variable setting/check may be taking place before the actual "Submit" action occurs. 

Aditya_hublikar
Mega Sage

Hello @deepakmaury ,

 

If you make variable mandatory from variable settings then your written only alert without checking variable has value or not then it will not show alert msg , please create catalog client script onSubmit() as shown in below and disable that variable setting mandatory option.

 
 

12.PNG

 

This above screenshot code will stop submission and show alert.

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya

yashkamde
Kilo Sage

Hello @deepakmaury ,

 

I would recommend avoid mixing mandatory flags with custom submit alerts. Mandatory flags are designed to stop submission automatically. If you need a tailored message, handle it entirely in script logic without the mandatory flag as given by @Aditya_hublikar .
If you just want enforcement, rely on the platform's built‑in mandatory behavior it's more consistent and user‑friendly.

If my response helped mark as helpful and accept the solution.

svirkar420
Tera Guru

Hi @deepakmaury , This is happening because of how the ServiceNow platform working order works. Instead of making the variable mandatory make it non-mandatory and then add the given script - 

Script -

function onSubmit() {

var value = g_form.getValue('variable_name'); // add your variable name

if (!value) {
alert('Please fill in the required field before submitting.');
return false; 
}

return true;
}

And if you want to keep that variable mandatory then ServiceNow itself won't let you submit the form unless you enter the value in the variable. This will also show the error message of the field is not filled with any value and restrict user before submission. So, there will be no need to add the cat client script for enforcing the value before submission. 

 

@deepakmaury  If this response helped resolve your issue, please consider marking it as Accepted Solution and giving it a 👍.
This helps others in the community find useful answers more easily.

Regards,
Saurabh V.