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

@is_12 

you will require 2 onChange client scripts i.e. 1 for account and 1 for contact name

both the client script will have similar scripts and will show error on respective field/variable when validation fails.

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

@OlaN @Ankur Bawiskar 

Requirement is to 

  1. when agent is ordering the item from configurable workspace then write a logic as follows.
    1. On change of primary contact, fetch the primary contact's account and then go to RITM table and check for the catalog item and the account fetched from primary contact. If there is an existing record then restrict the agent from ordering and also display the alert message "You are not allowed to order as there is an existing request for this customer"

@is_12 

I already suggested to use 2 onChange client scripts

When you use onSubmit with GlideAjax, there are challenges that the form gets submitted by the time the ajax function returns the value.

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

@Ankur Bawiskar one is on the contact & other one you are telling on the account(account is a readonly field)

 

@is_12 

if account is readonly then only 1 onChange client script i.e. on Contact should be sufficient

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