- 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:14 AM
Hi Imran,
Can you use below with added alerts & confirm the results or make sure if the values passed in 'if' statement are correct.
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');
alert('Service type is ' +service);
var phone = g_form.getValue('phone_type');
alert('Phone is '+phone);
alert('User Type is '+userType);
if (userType != 'Corning Employee' && service == 'new' && phone == 'personal_device')
{
g_form.addErrorMessage("This page is restricted to employees only.");
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:25 AM
Hi Jaspal.. The If conditions are correct, i get the error message as well.. I tried to put in the alerts which you mentioned, so the values seems to have met the condition.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2020 02:29 AM
In that case, you can look for Before insert business rule & do an abort action OR make it work using onChange() client scripts.
- 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