OnSubmit Return false depending on function outcome

Erik29
Kilo Expert

I have piece of Javascript which i can't seem to get working. It's not really rocket sience i guess. The Script Include is giving the values i want so there is no issue with that but. I want to check if user_name is not empty. (Which works if i add an info message). And when it's empty i want to return false on the on submit so that they can't proceed with this form. But for some reason the return isn't working properly and it just continues to the next screen and than prompt the error.

I need this functionality because i've created some HTML widgets on the form which i can't make mandatory like a variable. 

function onSubmit() {
    var ga = new GlideAjax("idCheck");
    ga.addParam("sysparm_name", "idCheck");
    ga.addParam("sysparm_user", g_form.getValue("colleague"));
    ga.getXMLAnswer(gaParse);

}

function gaParse(response) {
    var myJSON = JSON.parse(response);
    var id = myJSON.user_name;
	if (id == "") {

        g_form.addErrorMessage("No User ID Created");
        return false;
    } else {

        g_form.addInfoMessage(id);
        return true;
    }
}

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

Regards
Ankur

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

View solution in original post

4 REPLIES 4

ChrisBurks
Mega Sage

GlideAjax call is asynchronous. The onSubmit won't wait for an asynchronous call's return.
Is this a catalog form?

 

Yes it is indeed. 

ChrisBurks
Mega Sage

If it's a record producer you can do your check in the script portion of the Record Producer record and abort there with current.setAbortionAction(true).

If aborted it will remain on the catalog form and you can display an error for them to complete what's needed.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

Regards
Ankur

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