User criteria shows 'Item is unavailable for this user' error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am trying to limit the view of a catalog item, with user criteria, only to users that are part of any group that is available in the first variable of the item (see the attached screenshot). For some users I get the error 'Item is unavailable for this user' and for the users that are part of some groups available in the reference qualifier, the item is not visible at all. I am using the same reference qualifier for the user criteria (see the code below) as for the group variable. The reference qualifier is only based on types of groups.
I have also changed the access_type to delegated based on a post I saw on the community but that doesn't seem to solve the issue.
(function() {
gs.info('##test1');
var gr = new GlideRecord('item_option_new');
gr.addQuery('sys_id', 'x');
gr._query();
var queryStr = '';
if(gr._next()) {
queryStr = gr.getValue('reference_qual_condition');
gs.info("##test2 " + queryStr);
}
var groups = [];
var grGroup = new GlideRecord('sys_user_group');
grGroup.addEncodedQuery(queryStr);
grGroup._query();
while (grGroup.next()) {
groups.push(grGroup.getUniqueValue());
}
return groups;
})();Could you please help me in solving this issue? Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
In your user criteria script, you're not checking group membership and you're returning a list of groups based on the reference qualifier from your variable instead of true/false.
The below script works in my PDI and should work for you, just make sure you replace SYS_ID with the sys_id of the variable you're pulling the reference qualifier from. A few recommendations, ideally, you shouldn't use the reference qualifier on a variable to do anything within your user criteria; your user criteria should be able to stand alone. I would also try to avoid doing two GR queries as I've outlined below, but I don't know what other conditions are included on your variable reference qualifier in order to give you advice on consolidating you're querying to the group membership table only.
As a side note, ServiceNow says that scripted user criteria isn't cached and is evaluated every but that hasn't been my experience when testing user criteria for catalog items. During your testing, ensure you clear your cache and verify the criteria script isn't working before you make any changes to it.
answer = checkGroupMembership();
function checkGroupMembership() {
var groupList = [];
var isGroupMember = false;
var groupVariableRec = new GlideRecord('item_option_new');
groupVariableRec.get(SYS_ID);
var queryStr = groupVariableRec.getValue('reference_qual_condition');
var group = new GlideRecord('sys_user_group');
group.addEncodedQuery(queryStr);
group.query();
while(group.next()){
groupList.push(group.sys_id.toString());
}
var groupMembership = new GlideRecord('sys_user_grmember');
groupMembership.addEncodedQuery('group.sys_idIN' + groupList + '^user=' + user_id);
groupMembership.query();
if(groupMembership.hasNext()){
isGroupMember = true;
}
return isGroupMember;
}- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
why are you relying on the reference qualifier on some variable?
why not use that logic directly in script?
is that a variable of type Requested for directly added on your catalog item?
if yes then good explanation here for that variable type and it's behavior
Did you know? "Requested For" Variable Type and it's lesser known features!
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
