How to query the user table to return the users who fall under certain categories

gnunez
Kilo Guru

Hello all,

I am trying to make a catalog item available to certain users. I was recommended to use the advanced check box under user criteria and query the user table to return the users who fall under certain categories. I can do this in two ways, first we have a field in our user profiles called "Primary Security Affiliation" and I am trying to exclude if those users are under Student, Student Assistant, Student Applicant, or I can also exclude those without a job code since our students have that as empty.

Anyone who guide me in the right direction because I'm new with all the scripting. We are on Istanbul UI16.

Thanks in advance,

Grace

1 ACCEPTED SOLUTION

Use this code, it should work now



checkUsers();


function checkUsers(){


var gr= new GlideRecord('sys_user');


gr.addEncodedQuery('<add your encoded query here>'); //here you will need to set you conditions in the filter and then copy the query. refer section3 in this link Encoded Query Strings - ServiceNow Wiki


gr.addQuery('sys_id',gs.getUserID());


gr.query();


if(!gr.hasNext()){


      return true;


}


else{


      return false;


}


}


View solution in original post

20 REPLIES 20

I added the new line but still didn't do anything. I also tried adding it for catalog item as well as the category but it is still visible to students.


chirag_bagdai
ServiceNow Employee
ServiceNow Employee

try return true; or return false instead answer


That is weird, I have tested that on my instance and it was working. Try this variation and see if it works


Use this



checkUsers();


function checkUsers(){


var gr= new GlideRecord('sys_user');


gr.addEncodedQuery('<add your encoded query here>'); //here you will need to set you conditions in the filter and then copy the query. refer section3 in this link Encoded Query Strings - ServiceNow Wiki


gr.addQuery('sys_id',gs.getUserID());


gr.query();


if(gr.hasNext()){


      return true;


}


else{


      return false;


}


}


By the way, did you add this criteria in the Available for related list on the catalog item?


Thats strange if it worked for you it should work for me as well. Yes I added the criteria for both the catalog item and the category.



This is what it looks like:


find_real_file.png



Does yours look any different? I'm going to try the other script you pasted.