Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

HarshR158979806
Tera Guru

Hi @Amit Dey1 

Below code will work:

 

function onSubmit() {
	if (!g_form.ajaxComplete) {
		makeAjaxCall();
		//if the 
		return false;
	}
	return true;
}

function makeAjaxCall() {
    // If the flag is true, skip validation and allow form to submit
    

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

 

Mark it as Helpful it it helps you
Thanks,
Harsh Raghav

HarshR158979806
Tera Guru

Hi @Amit Dey1 

Try below code, it will work

function onSubmit() {
    if (!g_form.ajaxComplete) {
        makeAjaxCall();
        return false;
    }
    return true;
}

function makeAjaxCall() {
    // If the flag is true, skip validation and allow form to submit
    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
                g_form.ajaxComplete = true;
                g_form.submit();
            }
        });
        return false; // Stop submission until callback finishes
    }
}

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

 

Mark my response Helpful if it helps you
Thank you!!