How to show only Catalog items that are available for logged in users in Virtual agent. catalog item search

manikanta1258
Tera Contributor

I've created a topic Create a ticket or request in Virtual agent. I've used catalog item search topic block to get the catalog item we have searched with user input text. But it is showing all the item available in the catalog. 

I would like to see only the catalog items should be shown for which the user has access.

@Ankur Bawiskar 

Regards,

Manikanta Vankayala.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are asking user to input catalog item name?

If you want to show catalog items which user has access then you can use User Criteria API and use script and check which catalog items are satisfied and return those

UserCriteriaLoader - API to Evaluate User Criteria

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are asking user to input catalog item name?

If you want to show catalog items which user has access then you can use User Criteria API and use script and check which catalog items are satisfied and return those

UserCriteriaLoader - API to Evaluate User Criteria

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

something like this

1) you can use Table output as Bot response with this script

(function execute(table) {

var allCriterias = new sn_uc.UserCriteriaLoader.getAllUserCriteria(gs.getUserID());

var arr = [];

var gr = new GlideRecord("sc_cat_item_user_criteria_mtom");
gr.addQuery("user_criteria", "IN", allCriterias);
gr.query();
while (gr.next()) {
	arr.push(gr.getValue('sc_cat_item'));
}

var gr = new GlideRecord(table);
gr.addEncodedQuery('sys_id', "IN", arr.toString());
gr.query;
return gr;

})(table)

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@manikanta1258 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Thanks for responding.

Yes I'm receiving the input from user and By using OOTB Catalog item Search Topic Block.

Here is the script used in that Search.

(function execute() {
//get from inputs passed to this block
var search_term = vaInputs.query_term.getValue();
var catalogIds = vaInputs.catalogs.getValue();
vaVars.limit = vaInputs.result_limit.getValue() || '5';

//Filter out any catalog items that should not be shown ie. Record Producers
//create a filterMap to quickly see if item can be shown to user
var filter_tables = vaVars.filter_tables.split(',');
var filterMap = {};
for (var i=0; i < filter_tables.length; i++) {
filterMap[filter_tables[i]] = 'true';
}

var gr = sn_sc.CatalogSearch.search(catalogIds, '', search_term, false, false, false);
gr.query();
var count = 0;
var results = [];
while (gr.next() && results.length < parseInt(vaVars.limit)) {
var catItemId = gr.getValue('sys_id');
var catItemObj = new sn_sc.CatItem(catItemId);
var category = catItemObj.getFirstAccessibleCategoryForSearch(catalogIds);
if (!catItemObj.canViewOnSearch() || !category)
continue;
var catItemJson = catItemObj.getItemSummary(false, 'basic');

if (filterMap[catItemJson.sys_class_name] === undefined) {
results.push({
'sys_id': catItemId,
'title': catItemJson.name,
'image': catItemJson.picture,
'sys_class_name': catItemJson.sys_class_name
});
count++;
}
}
vaVars.count = results.length;
vaVars.results = JSON.stringify(results);

return'';
})()
 
I want to add another condition to show the catalog item only if the user is there in User criteria in Available for.
 
Will wait for your response.
 
Regards,
Mani.