palanikumar
Giga Sage

You should update the logic in a way that form gets submitted only if the response is true. You can use this code

 

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);
        return false; // Cancel the form submission
    }
}
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'));
    } else {
        alert("else if alert");
        g_form_submit(); // Submit the form only if the response is true
    }
}
Thank you,
Palani

@palanikumar I need to disable the button order now button

 

Thanks,

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