Lookup select box that's dependent on a user's user criteria

evieta
Tera Contributor

I created an order guide that accumulates the 10 cat items. I created a lookup select box on the u_custom_catalog_item_choices that will display 10 choices of the cat items which are named more user-friendly. Since these cat items have different user critieria, I want to display the choices that are only avaialable to the user. So I created a script include to look at the user critieria of the currently logged in user and add the choices based on that. When I call it in the reference qualifier, the 10 choices still appear under the dropdown even though I impersonate a user that for sure will not see those forms. I'm not sure if it's because of a bug in my script or if the variable attributes is empty. Any advice is appreciated!

 

evieta_0-1696980602759.png

 

 Here's the script include function :

testing : function() { 
		var catItemNames = ['7f78c78f4f8017408147fc828110c78e', '1e4217594ff41a009dec32718110c781', '2f943e6ddb2f7300cc1f27360596190f','e8999c66dbc5d450593241db13961986', '0d74c1f77c9579006329393899c4ac5f', 'df52cb7adb959050d2c00bb6f496195d', '6cc2f9724fb92f009dec32718110c747', '4c55a02520513d002145c562e4fb7dbc', 
'118de21d13cda7007eb357b32244b0e8', 'c9c19367dba36050cc1f2736059619cf'];
        
		var valueName = ['choice1', 'choice2', 'choice3', 'choice4','choice5',
						'choice6', 'choice7', 'choice8','choice9', 'choice10'];
		
        var start = 0;
        var tableSize = catItemNames.length;
        var refQualifier = '';
		var result = [];

		while (start < tableSize) {
			var notAvailableForTable = new GlideRecord('sc_cat_item_user_criteria_no_mtom');
            notAvailableForTable.addQuery('sc_cat_item.sys_id', catItemNames[start]);
            notAvailableForTable.query();

            var availableForTable = new GlideRecord('sc_cat_item_user_criteria_mtom');
            availableForTable.addQuery('sc_cat_item.sys_id', catItemNames[start]);
            availableForTable.query();

			var naArray = [];
			var aArray = [];
			var countA = 0;
			var countNA = 0;
			while (notAvailableForTable.next()) {
				naArray.push(notAvailableForTable.user_criteria.sys_id);
				if (SNC.UserCriteriaLoader.getAllUserCriteria().contains(notAvailableForTable.user_criteria.sys_id)) {
					++countA;
				}
            }

			while (availableForTable.next()) {
                aArray.push(availableForTable.user_criteria.sys_id);
				if (SNC.UserCriteriaLoader.getAllUserCriteria().contains(availableForTable.user_criteria.sys_id)) {
					++countNA;
				}
            }

			
			if (countA > 0) {
                result.push(valueName[start]);
            }
			else if (countA == 0 && countNA == 0) {
                result.push(valueName[start]);
			}
            ++start;
		}

		return 'u_valueIN' + result.join(',');
	},

 

1 ACCEPTED SOLUTION

athm
Tera Guru

I think this might suit your use case, since you are checking catalog item access for current logged in users.
I took the reference from 
https://www.servicenow.com/community/now-platform-forum/how-to-script-a-user-criteria-check/m-p/1200...

I hope this helps you.

 

//I think this might suit your use case

var itemsToCheck = [
'7f78c78f4f8017408147fc828110c78e', '1e4217594ff41a009dec32718110c781', '2f943e6ddb2f7300cc1f27360596190f','e8999c66dbc5d450593241db13961986', '0d74c1f77c9579006329393899c4ac5f', 'df52cb7adb959050d2c00bb6f496195d', '6cc2f9724fb92f009dec32718110c747', '4c55a02520513d002145c562e4fb7dbc', 
'118de21d13cda7007eb357b32244b0e8', 'c9c19367dba36050cc1f2736059619cf'
];

var availableItemsToUser = [];

for(var i=0; i<itemsToCheck.length; i++){
var item = GlideappCatalogItem.get(catItem.sys_id);
if (item != null && item.canView()) {
availableItemsToUser.push(item + '');
}
}
return 'sys_idIN' + availableItemsToUser;


View solution in original post

2 REPLIES 2

athm
Tera Guru

I think this might suit your use case, since you are checking catalog item access for current logged in users.
I took the reference from 
https://www.servicenow.com/community/now-platform-forum/how-to-script-a-user-criteria-check/m-p/1200...

I hope this helps you.

 

//I think this might suit your use case

var itemsToCheck = [
'7f78c78f4f8017408147fc828110c78e', '1e4217594ff41a009dec32718110c781', '2f943e6ddb2f7300cc1f27360596190f','e8999c66dbc5d450593241db13961986', '0d74c1f77c9579006329393899c4ac5f', 'df52cb7adb959050d2c00bb6f496195d', '6cc2f9724fb92f009dec32718110c747', '4c55a02520513d002145c562e4fb7dbc', 
'118de21d13cda7007eb357b32244b0e8', 'c9c19367dba36050cc1f2736059619cf'
];

var availableItemsToUser = [];

for(var i=0; i<itemsToCheck.length; i++){
var item = GlideappCatalogItem.get(catItem.sys_id);
if (item != null && item.canView()) {
availableItemsToUser.push(item + '');
}
}
return 'sys_idIN' + availableItemsToUser;


evieta
Tera Contributor

wow, this definitely condensed the code! thank you