- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:11 AM
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;
}
}
Solved! Go to Solution.
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:30 AM
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
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:16 AM
GlideAjax call is asynchronous. The onSubmit won't wait for an asynchronous call's return.
Is this a catalog form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:16 AM
Yes it is indeed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:23 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 07:30 AM
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
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader