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

Hi Ankur.. Thanks.. Both the alerts are getting triggered. getReference() with callback might be the case.

What could be the solution?

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)

 

 

Hi Imran,

As suggested by @Jaspal Singh & @Harshvardhan you can try either of these 2 approaches

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

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

Ct111
Tera Sage

Hello,

I think your case is similar to below link...

https://community.servicenow.com/community?id=community_question&sys_id=a78c62e3db29e300107d5583ca96...

 

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