Not Available for user criteria script is not working

tpeleg
Tera Expert

Hello experts,

 

I'm trying to hide a catalog item for specific users, based on sys_user string field.

I've created not available for  user criteria with a simple script, but for some reason it doesn't work (relevant user are still able to see -  even after cleaning cache and log out login).

I've found some articles about the gs.getUser issues within the user criteria advance script. that suggest to use extend user criteria but my field is string and not a reference.

 

my script:

 

answer();
function answer() {
var user_id = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.get(user_id);
var userSource = gr.getValue('state');
if (userSource == "SW") {
return true;
} else {
return false;
}
}
 

Did someone find a solution for this issue?

 

Thanks!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@tpeleg 

did you debug it by adding gs.info()?

I hope the field name state is correct, since state field is not present OOB

Also update script as this. The answer variable needs to be set with true/false

If user's state value is SW it won't be available for

answer = checkUserRecord();
function checkUserRecord() {
	var gr = new GlideRecord('sys_user');
	if(gr.get(user_id)){
		var userSource = gr.getValue('state');
		if (userSource == "SW") {
			return true;
		} else {
			return false;
		}
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@tpeleg 

did you debug it by adding gs.info()?

I hope the field name state is correct, since state field is not present OOB

Also update script as this. The answer variable needs to be set with true/false

If user's state value is SW it won't be available for

answer = checkUserRecord();
function checkUserRecord() {
	var gr = new GlideRecord('sys_user');
	if(gr.get(user_id)){
		var userSource = gr.getValue('state');
		if (userSource == "SW") {
			return true;
		} else {
			return false;
		}
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

tpeleg
Tera Expert

Thank you Ankur & Prince!