- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 02:26 PM
<<<-------------------------------------------------------->>>>>
Hi collogues,
I have written a catalog onSubmit() client script as above to validate the field Requested for "u_requested_for" (type = single line). If the logged in user enters his own userid and select the choice from the field Raising on behalf ("u_raising_on_behalf_of") (type= select box) the field as others("u_others") then the submission of the form should be stopped with errormessage but if any one of the conditions fails, then the form should be submitted.
Here the validation part is working fine (Submission is prevented) but when it goes to else part (step5) instead of allowing the submission only once, it's going into infinite loop and again starting from step 1. Any one faced similar issue . Thanks in advance .
attaching the snip of the script to avoid any confusion
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2025 04:51 PM - edited 05-28-2025 04:54 PM
Calling g_form.submit(); submits your form again so the onSubmit() handler will run again. You need some sort of persistent variable that you can set and then read within your handler to understand if the form has been validated or not, and then return true to continue normal form submission from your onSubmit handler if it's been set.
First, update your callback to set a property on g_scratchpad (this is the persistent property that we can read on the next execution of your onSubmit to prevent an infinite loop):
// important: getActionName() must be called synchronously (not within the callback)
var actionName = g_form.getActionName();
ga.getXMLAnswer(function(response) {
...
else {
alert('Step 5: User Name does not match, proceeding with submission');
// add a property to g_scratchpad so that we can read it when `onSubmit()` fires again
g_scratchpad.formValidated = true;
g_form.submit(actionName);
}
});
Then at the top of your script, prevent the infinite loop and return true to continue with normal form submission if the formValidated property we set within the callback is set:
function onSubmit() {
if (g_scratchpad.formValidated) // continue with normal form submission if form has been validated
return true;
// ... rest of your code ...
}
This is also the approached suggested by ServiceNow in KB0783579 - How to do async validation in an onsubmit client script. for dealing with asynchronous form validation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 07:57 AM
Hello @rajtiwari02 ,
Your script is using "g_user.getUserName()" but there is no such method. That's why it is not working.
You need to use "g_user.userName".
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 07:06 AM
Hi @Robert H , thanks for the solution but I am using Service portal for the form submission even after trying that only I posted the concern on community. Thanks all for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 07:25 AM - edited 05-30-2025 07:26 AM
Hello @rajtiwari02 ,
I don't understand. g_form.userName works in Service Portal as well.
I'm not saying that the other solutions are incorrect. But for your particular requirement using AJAX and Script Includes just to get the user ID is unnecessarily complex.
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 07:41 AM
Hi @Robert H , I understood your point but the script you provided there "g_user" has been used. Your recent output is correct with g_form.username. Please refer below my previous code which was not working as I was using "g_user" API same as you have used above in service portal client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 07:57 AM
Hello @rajtiwari02 ,
Your script is using "g_user.getUserName()" but there is no such method. That's why it is not working.
You need to use "g_user.userName".
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2025 08:05 AM
Hi @Robert H
Thanks for your help. Yes, it's working. Actually, I had limited time, so I tried the random solution. But yes, its correct. thank you all, have a great day.