I have created an onSubmit cat. Client Script to show an alert if a field is not filled by the user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
