User Criteria script is not working

ramesh_r
Mega Sage

Hi All,

I have written this User Criteria on article Level this script in executing and its going to inside the IF condition but is not working as expected Please help me to resolve this issue

 

checkRegion();
function checkRegion() {

var s = gs.getUser().getRecord().getValue('u_country');
gs.addErrorMessage(s);
if(s == "IND")
{
answer = true;
}
else
{
answer = false;
}

 

User Criteria :-

find_real_file.png 

KB Article 

find_real_file.png

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Ramesh,

Can you please try the following code in your user criteria:

answer = false; // Default to false

var userRec = new GlideRecord("sys_user");
if (userRec.get(gs.getUserID())) {
	var s = userRec.getValue('u_country');

	gs.addErrorMessage(s);
	if(s == "IND") {
		gs.addInfoMessage("My Country is: " + s);
		answer = true;
	}
}

Let me know if it worked for you.

Brent

P.S. Please mark as correct if this information helped you so the rest of the community can benefit.

View solution in original post

10 REPLIES 10

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I have seen issues sometimes with nested functions to the getUser().  Try this instead:

checkRegion();

function checkRegion() {
	var userRec = new GlideRecord("sys_user");
	userRec.get(gs.getUserID());
	var s = userRec.getValue('u_country');
	
	gs.addErrorMessage(s);
	if(s == "IND")
	{
	answer = true;
	}
	else
	{
	answer = false;
}

Hi Michael,

 

I have changed the code but its not working for both USA and IND user, its showing below error for all users

find_real_file.png

 

User List

find_real_file.png

Doh, the function never returns the answer!  Try this:

checkRegion();

function checkRegion() {
	var userRec = new GlideRecord("sys_user");
	userRec.get(gs.getUserID());
	var s = userRec.getValue('u_country');
	
	gs.addErrorMessage(s);
	if(s == "IND")
	{
	answer = true;
	}
	else
	{
	answer = false;
	
	return answer;
}

Actually also noticed the answer variable was never declared, sorry, use this one instead:

checkRegion();

function checkRegion() {
	var answer = false;  // Default to false
	
	var userRec = new GlideRecord("sys_user");
	userRec.get(gs.getUserID());
	var s = userRec.getValue('u_country');
	
	gs.addErrorMessage(s);
	if(s == "IND")
	{
	answer = true;
	}
	
	return answer;
}