using "CONTAINS" or indexof in a User Criteria script

kevinray
Giga Expert

Hi all,

I'm trying to restrict the visibility of a catalog item (Available To) to only people that belong to a Cost Center that contains 5471 or 5400. I created a "User Criteria" record tried two ways, (well three if you count me trying to use .name or not using it) and neither seems to work.

NOTE: the field on the user record is cost_center and is a reference to the cmn_cost_center table. Our cost centers look like this: 5471AB, 5471TA, 6520AB, 5400AB, etc..

Tried this

var userCheck = new GlideRecord("sys_user");    

userCheck.addQuery("user", gs.getUserID());    

userCheck.addQuery("cost_center.name", "CONTAINS" , "5471");

userCheck.addOrCondition("cost_center.name", "CONTAINS", "5400");

userCheck.query();    

if (userCheck.hasNext()) {    

      answer = true;    

} else {  

      answer = false;  

}  

Doesn't work. Doesn't restrict anyone from seeing it.

and have tried

var userCheck = new GlideRecord("sys_user");    

userCheck.addQuery("user", gs.getUserID());    

userCheck.addQuery("cost_center", "CONTAINS" , "5471");

userCheck.addOrCondition("cost_center", "CONTAINS", "5400");

userCheck.query();    

if (userCheck.hasNext()) {    

      answer = true;    

} else {  

      answer = false;  

}  

Doesn't work. NO one can see it (only admins)

and have tried

costCenterVerify();

function costCenterVerify() {

  var userCC=gs.getUser().getRecord().getDisplayValue('cost_center');

  if (userCC.indexof("5471") > -1 || userCC.indexof("5400") > -1) {

  answer = true;

  } else {

  answer = false;

  }

  return answer;

}

Doesn't work. NO one can see it (only admins)

Neither working. Any ideas?

1 ACCEPTED SOLUTION

I Figured it out...Just needed to keep trying



var gr = new GlideRecord('sys_user');    


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


gr.addEncodedQuery('cost_centerLIKE5471^ORcost_centerLIKE5400');


gr.query();    


if (gr.hasNext()) {    


      answer = true;    


} else {  


      answer = false;  


}  


View solution in original post

2 REPLIES 2

kevinray
Giga Expert

Thought i'd also show you want the field looks like on the user record



find_real_file.png


I Figured it out...Just needed to keep trying



var gr = new GlideRecord('sys_user');    


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


gr.addEncodedQuery('cost_centerLIKE5471^ORcost_centerLIKE5400');


gr.query();    


if (gr.hasNext()) {    


      answer = true;    


} else {  


      answer = false;  


}