The CreatorCon Call for Content is officially open! Get started here.

"return false;" not working onSubmit

imran rasheed
Tera Contributor

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;
}
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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
 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Jaspal Singh
Mega Patron
Mega Patron

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;
}
}

 

 

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.

In that case, you can look for Before insert business rule & do an abort action OR make it work using onChange() client scripts.

Ankur Bawiskar
Tera Patron
Tera Patron

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
 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader