stopping catalog form submission
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
I have created a catalog onsubmit client script where we are checking if 'product name' field value is same for any of the record in backend table then we need to cancel catalog form submission but it is not working
this is the code-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Hi @Amit Dey1 ,
Your script fails because GlideAjax runs asynchronously, so the form submits before the callback returns. You need to stop submission first, run the check, and only call g_form.submit() from the callback if the product name doesn’t exist. That way, the validation actually prevents submission.
Please check below code
function onSubmit() {
var actionType = getParmVal('Action_Type');
if (actionType == 'New') {
var ga = new GlideAjax("global.AIRProductClientUtils");
ga.addParam("sysparm_name", "checkProductNameExist");
ga.addParam("sysparm_nameValue", g_form.getValue('cat_product_name'));
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
g_form.clearValue("cat_product_name");
g_form.showErrorBox('cat_product_name', 'Product Name already in use');
alert('Product Name already in use');
return false;
}
}
}
function getParmVal(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(top.location);
if (results == null) {
return "";
} else {
return unescape(results[1]);
}
}
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Additionally you can also give gs.sleep() in script include. So you client script will wait for the response.
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
this is the modified script , it is not submitting the form for that condition but even with a new name it is preventing from submission
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Hi @Amit Dey1
as you are writing DOM script so please unCheck the isolate script checkbox.
DOM is not a Best practice.. can you expalin your use case
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
