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

Patrick Fedigan
Giga Guru

Hello Grace,



Have you heard of Access Control Rules? Do non students have roles that can distinguish them from students? I would see using an ACL as best practice rather than a script.


Here is page to read a little bit about them



http://wiki.servicenow.com/index.php?title=Using_Access_Control_Rules#gsc.tab=0



Cheers


Hi Patrick,



I have but haven't used them yet. Yes non-students are assigned the itil role but others such as faculty, don't have any role assigned to but should still see the catalog item. This is where it gets a bit complicated.



Thanks, I will take a look at ACLs


Abhinay Erra
Giga Sage

Put this in the advanced section




var user= new GlideRecord('sys_user');


user.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  


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


usr.query();


if(user.hasNext()){


      answer = true ;


}


else{


      answer = false ;


     


}


answer ;


Hi Abhinay,



I tried the code below and its not working. Am I doing something wrong?



find_real_file.png


you need to add line answer; after line 10