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

OlaN
Giga Sage
Giga Sage

Hi,

GlideAjax calls are run async, so the script is completed and the form is submitted before you get the answer back from the call. Switch your script to run as an onChange client script instead of the onSubmit one.

is_12
Tera Contributor

@OlaN in onchange, How to retrict the form submission 

I'm guessing you are restricting the form based on that you have one (or more) mandatory field to fill in.

For example the user selected.

When changing the user, do the check with the script, and if the user is not allowed, empty the field, so another user must be selected.

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

you should use onChange client script and show error message for that field

This will ensure user passes the validation and selects the correct info and then only form will get submitted.

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