- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:08 AM
I am using the below script for onSubmit. The If condition gets passed through and am getting the error message "This page is restricted to employees only." But, the ticket gets submitted. Return false is not working.
function onSubmit() {
var user = g_form.getReference('requested_for', setUserType);
}
function setUserType(user){
//var userType = user.u_user_type;
var userType = user.u_user_type;
var service = g_form.getValue('service_type');
var phone = g_form.getValue('phone_type');
if (userType != 'Corning Employee' && service == 'new' && phone == 'personal_device'){
g_form.addErrorMessage("This page is restricted to employees only.");
return false;
}
}
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:17 AM
Hi Imran,
getReference() with callback is asynchronous I think it is submitting the form by the time your callback method triggers;
If this doesn't work move your validation as onchange client script on service type and phone type variable/field
To confirm this add alerts
function onSubmit() {
var user = g_form.getReference('requested_for', setUserType);
}
function setUserType(user){
//var userType = user.u_user_type;
alert('CallBack method called');
var userType = user.u_user_type;
var service = g_form.getValue('service_type');
var phone = g_form.getValue('phone_type');
if (userType != 'Corning Employee' && service == 'new' && phone == 'personal_device'){
alert('Stop form');
g_form.addErrorMessage("This page is restricted to employees only.");
return false;
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
05-13-2020 02:22 AM
Hi Ankur.. Thanks.. Both the alerts are getting triggered. getReference() with callback might be the case.
What could be the solution?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:27 AM
it wont work. because asynchronous call is happening .
you have to use here onChange client script , if you are on table then use before business rule using setAbortAction(true)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:40 AM
Hi Imran,
As suggested by
1) if this is for record producer then before insert BR on the target table of record producer
2) if this is for catalog then onchange client script
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
05-13-2020 02:18 AM
Hello,
I think your case is similar to below link...
Please , check it is worth noting what has been said in below link regarding getReference.
Mark my ANSWER as CORRECt and HELPFUL if it works