- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2023 06:30 PM
Hello All,
I'm facing an issue with validation in onSubmit catalog client script. Went through different articles, but couldn't arrive at a concrete solution for this. Any help is greatly appreciated.
TIA
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2023 07:11 AM
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
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2023 11:04 AM
Hi @muresh
As Ankur stated, if you are planning to use GlideAjax in onSubmit Catalog Client Script, you need to tweak the system a bit as getXMLWait will not work in onSubmit. You can use getXML or getXMLAnswer. For example,
function onSubmit() {
if (g_scratchpad.isFormValid){
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax("Your_Script_Include");
ga.addParam("sysparm_name", "YourMethodName");
ga.getXMLAnswer(function(answer) {
if(answer == 'true'){
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}else{
g_scratchpad.isFormValid = false;
}
});
return false;
}
By default we prevent form submit by using return false; Here we use g_scratchpad to store the validation status. And then we modify this status based on the async call return value. If the validation passes, we will submit the form using action name.
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2023 07:11 AM
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
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:11 AM
Thanks Ankur for your response. Now I understand these things better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2023 11:04 AM
Hi @muresh
As Ankur stated, if you are planning to use GlideAjax in onSubmit Catalog Client Script, you need to tweak the system a bit as getXMLWait will not work in onSubmit. You can use getXML or getXMLAnswer. For example,
function onSubmit() {
if (g_scratchpad.isFormValid){
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax("Your_Script_Include");
ga.addParam("sysparm_name", "YourMethodName");
ga.getXMLAnswer(function(answer) {
if(answer == 'true'){
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}else{
g_scratchpad.isFormValid = false;
}
});
return false;
}
By default we prevent form submit by using return false; Here we use g_scratchpad to store the validation status. And then we modify this status based on the async call return value. If the validation passes, we will submit the form using action name.
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 01:10 AM
I tried this one and with little changes I managed to make my requirement work. Thanks for the example.