- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 03:45 AM
Hi Everyone,
i've created an OnChange client script on a variable set, the script should return false if the age (through DOB is more than 21 )and should not allow the submission of the record producer, but return false; is not working.
the script throws alert if age is more than 21 , but it still submits the form, i want to prevent form submission.
it throws the error but takes in the value under the variable set.
any support would be much appreciated.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 07:53 AM - edited 02-12-2024 07:54 AM
Hi @kartikey,
I've tested below script as onSubmit script on the variable set as working in my instance:
So this will work for every row submitted in your MRVS separately. It will not work in this form on the catalog item.
function onSubmit() {
if (g_scratchpad.isFormValid)
return true;
var dob = g_form.getValue('date_of_birth');
alert(dob);
var ga = new GlideAjax('CallerDeatils');
ga.addParam('sysparm_name', 'getAge');
ga.addParam('sysparm_dob', dob);
ga.getXML(callBackFunction);
return false;
}
function callBackFunction(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer > 21) {
g_form.addErrorMessage('test');
return false;
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 07:53 AM - edited 02-12-2024 07:54 AM
Hi @kartikey,
I've tested below script as onSubmit script on the variable set as working in my instance:
So this will work for every row submitted in your MRVS separately. It will not work in this form on the catalog item.
function onSubmit() {
if (g_scratchpad.isFormValid)
return true;
var dob = g_form.getValue('date_of_birth');
alert(dob);
var ga = new GlideAjax('CallerDeatils');
ga.addParam('sysparm_name', 'getAge');
ga.addParam('sysparm_dob', dob);
ga.getXML(callBackFunction);
return false;
}
function callBackFunction(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer > 21) {
g_form.addErrorMessage('test');
return false;
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:05 AM
perfect! Works like a charm! 😄
but i observed that we need to click 'add' twice in order to add the date of birth variable , not sure why.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 05:16 AM
Hi @kartikey, as per my knowledge return false works only on OnSubmit client script but not on the OnChange client script. Also, the GlideAjax (async) may not work with OnSubmit client script.
Ref: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579
Regards,
Sunil