stopping catalog form submission

Amit Dey1
Tera Contributor

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-

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(callback);
    }
   
    function callback(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]);
        }
    }
}
4 REPLIES 4

Sarthak Kashyap
Kilo Sage

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

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

var allowSubmit = false;

function onSubmit() {
    // If the flag is true, skip validation and allow form to submit
    if (allowSubmit) {
        return true;
    }

    var fss = g_form.getValue('cat_funding_sources_and_structure');
    var fss_old = g_form.getValue('cat_fss_old');

    if (fss_old.length == 32 && fss.length == 0) {
        return confirm("Caution: Removing the AIM case will disassociate all products and applications from the funding source.");
    }

    var actionType = getParmVal('Action_Type');
    if (actionType == 'New') {
        var productName = g_form.getValue('cat_product_name');
        var ga = new GlideAjax("global.AIRProductClientUtils");
        ga.addParam("sysparm_name", "checkProductNameExist");
        ga.addParam("sysparm_nameValue", productName);

        // Stop the original submit until we know the result
        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');
            } else {
                // Allow form to submit now
                allowSubmit = true;
                g_form.submit();
            }
        });

        return false; // Stop submission until callback finishes
    }

    return true; // allow other action types
}

function getParmVal(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(top.location);
    return results == null ? "" : unescape(results[1]);
}
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

Ravi Gaurav
Giga Sage
Giga Sage

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/