checking for user criteria in category scripts

jasonalldridge
Kilo Expert

Hi All,

I have entries in the sc_category table with some of the categories having user criteria applied to them.

In a catalogue item I have a 'Parent category' variable - this is a reference field that points to that sc_category table. I need to be able to restrict the list of categories being displayed to only those that users are allowed to see, and could do with a pointer on ensuring user criteria is enforced.

I'm using an advanced reference qualifier with the following code (note query has been simplified for this post)

var query = 'active=true';

// how to add in check for user criteria???

var gr = new GlideRecord('sc_category');

gr.addEncodedQuery(query);

gr.query();

var categories =[];

while(gr.next()){

      categories.push(gr.getValue('sys_id'));

}

return 'sys_idIN' + categories;

Idea gratefully received!

2 REPLIES 2

larstange
Mega Sage

Hi



I have shared a solution I use which can match the current logged in user to a user criteria. Feel free to use and modify it: ServiceNow Share


robhumphries
Kilo Expert

You can do this with some of the classes added for the service portal.

class sn_sc.catItem allows you to check canView() which validates user criteria

if you want to do this for a whole category you can also use sn_sc.catCat getItemIds() and make a nested loop

an example nested loop would be:

// empty array to hold the sys_ids of items which pass user criteria
var okItemSysIds = [];

// variable to hold the sys_id of the category you want to check
var catId = 'yourCategoryID';

// get an array of ids of the items in this category
var catJS = new sn_sc.catCat(catID);
itemIds = catJS.getItemIds();

// loop through each item id
for(var i=0; i<itemIds.length; i++){

  var catItem = new sn_sc.catItem(itemIds[i]);

  // if they pass user criteria add them to the first array
  if(catItem.canView(gs.isMobile())){
    okItemSysIds.push(itemIds[i]);
  }
}