catalog form submission is getting stopped for asynchronous glideajax call

Amit Dey1
Tera Contributor

I have made below script for a certain condition where I want to stop catalog form submission and it is working fine but for any other time also form is not getting submitted. 

 

 

 

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]);
}
6 REPLIES 6

Nawal Singh
Tera Guru

Hi @Amit Dey1 ,

Please check below code- 

 

var allowSubmit = false;

function onSubmit() {
    // Allow submission if flagged
    if (allowSubmit) {
        allowSubmit = false; // reset for next submit attempt
        return true;
    }

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

    // Check for removal confirmation
    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 form submission until AJAX completes
        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 {
                // Temporarily allow submission and resubmit
                allowSubmit = true;
                g_form.submit();
            }
        });

        return false; // Stop the first submit
    }

    // For all other cases, allow normal submission
    return true;
}

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

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

same didn't work, can we modify the script with g_scratchpad?

yes we can if you want to pass some value from business rule.

 

Can you please debug the script where its falling?  and why we are not able to achieve as expected.

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

Hi @Amit Dey1 

 

var allowSubmit = false;

function onSubmit() {

// if allowed due to async callback, let it submit
if (allowSubmit) {
allowSubmit = false; // reset so next time it doesn't skip validation
return true;
}

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

// condition 1 - show confirmation warning
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.");
}

// condition 2 - product name uniqueness check
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);

// asynchronous call — block original submission
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 {
allowSubmit = true;
g_form.submit(); // re-submit only once
}
});

return false; // block original submission until callback
}

// if no condition met, allow submission normally
return true;
}

// helper to extract URL parameters
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]);
}

--------------------------------------------------------------------------------------------------------------------------


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/