Onsubmit script is not working with script

is_12
Tera Contributor

Hello Community !

 

function onSubmit() {
    if (g_user.hasRole('sn_customerservice_agent')) {
        var ga = new GlideAjax('catalogUtils');
        ga.addParam('sysparm_name', 'hasExistingRITM');
        ga.addParam('sysparm_account', g_form.getValue('account'));
        ga.addParam('sysparm_userId', g_form.getValue('contact_name'));
        ga.addParam('sysparm_catalogItem', g_form.getUniqueValue());
        ga.getXMLAnswer(setData);

    }

    function setData(response) {
        var answer = response;
        alert('reponse' + answer);
        if (answer == 'false') {
            alert(getMessage('You are not allowed to order as there is an existing request for this customer'));
           return false;
        } else {
            alert("else if alert");
            return true;
        }
    }
}
I'm getting the alert message but form is getting submitted.
Tried with getXMLWait(), it is getting supported
 
Thanks,
2 ACCEPTED SOLUTIONS

@is_12 

this worked for me in onLoad catalog client script

I hope I answered your question and you can enhance it further.

function onLoad() {
		var element = this.document.getElementsByClassName('btn btn-primary btn-block text-overflow-ellipsis ng-binding ng-scope');
		element[0].style.display = 'none';
}

AnkurBawiskar_0-1747748977910.png

 

Output:

AnkurBawiskar_1-1747748998896.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

23 REPLIES 23

sunil maddheshi
Tera Guru

@is_12 

You're using GlideAjax.getXMLAnswer(), which is asynchronous. This means the onSubmit() function completes before setData() finishes — so the form submission proceeds before the validation result is received.

If your goal is to validate the combination of account, contact_name, and catalog item whenever a specific field changes (e.g. contact_name or account), you can do this via onChange.

Here’s how to convert your script to an onChange client script (example: triggered when contact_name)

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    if (g_user.hasRole('sn_customerservice_agent')) {
        var ga = new GlideAjax('catalogUtils');
        ga.addParam('sysparm_name', 'hasExistingRITM');
        ga.addParam('sysparm_account', g_form.getValue('account'));
        ga.addParam('sysparm_userId', newValue);  // since contact_name is triggering
        ga.addParam('sysparm_catalogItem', g_form.getUniqueValue());

        ga.getXMLAnswer(function(response) {
            if (response === 'false') {
                g_form.showFieldMsg('contact_name', getMessage('You are not allowed to order as there is an existing request for this customer'), 'error');
            }
        });
    }
}

Please mark correct/helpful if this helps you!



 

Muhammad Salar
Giga Sage

Try this, button will not disable but request can not be submitted for false

function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}

var actionName = g_form.getActionName();

if (g_user.hasRole('sn_customerservice_agent')) {
var ga = new GlideAjax('catalogUtils');
ga.addParam('sysparm_name', 'hasExistingRITM');
ga.addParam('sysparm_account', g_form.getValue('account'));
ga.addParam('sysparm_userId', g_form.getValue('contact_name'));
ga.addParam('sysparm_catalogItem', g_form.getUniqueValue());

ga.getXMLAnswer(function(response) {
var answer = response;
alert('response: ' + answer);

if (answer == 'false') {
alert(getMessage('You are not allowed to order as there is an existing request for this customer'));
g_scratchpad.isFormValid = false;
} else {
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
});

return false;
}

return true;
}





ChiranjeeviR
Kilo Sage

Hi @is_12 

 

You cannot use asynchronous GlideAjax inside onSubmit() if you want to prevent submission.

 

Use getXMLWait() (Synchronous Call)

 

Since you said getXMLWait() is working, that’s the expected behavior, as it waits for the server response.

 

Note: ServiceNow discourages getXMLWait() in client scripts because it's synchronous and can cause the UI to freeze temporarily.

 

You can try this script:

function onSubmit() {
    if (g_user.hasRole('sn_customerservice_agent')) {
        var ga = new GlideAjax('catalogUtils');
        ga.addParam('sysparm_name', 'hasExistingRITM');
        ga.addParam('sysparm_account', g_form.getValue('account'));
        ga.addParam('sysparm_userId', g_form.getValue('contact_name'));
        ga.addParam('sysparm_catalogItem', g_form.getUniqueValue());
        
        var answer = ga.getXMLWait();
        var response = answer.responseXML.documentElement.getAttribute("answer");
        
        if (response == 'false') {
            alert(getMessage('You are not allowed to order as there is an existing request for this customer'));
            return false;
        }
    }
    return true;
}

 

Thanks and Regards,

Chiranjeevi R

Please mark as Correct Answer/Helpful, if applicable.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

is_12
Tera Contributor

@palanikumar @Ankur Bawiskar @ ChiranjeeviR @Muhammad Salar @sunil maddheshi 

Tons for Thanks for all your replay, It is working now