- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 10:22 PM
Hi,
I am creating topic in Virtual agent chat bot to search knowledge articles. I want to display search result by decreasing order of view count.
Could anyone please help to me with the script to display result by decreasing order of view count.
Regards,
Pinki
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2019 01:44 AM
Seemed a lot easier and quicker then I thought. Just giving you the thought + full code below.
First, I thought, let's check what the Contextual Search actually gives us. Notice the viewCount property.
So could we sort on this? Actually, meta.viewCount I don't know... though looking at the solution which we got for Order an Item (and posted in the previous post), we could add a count. So started testing this with background script (easier and quicker to test then kicking of the Virtual Agent topic everytime).
WORKS!!!
Our fullcode for the Contextual Search step:
(function execute() {
vaVars.indexInt = 0;
var searchtermStr = vaInputs.search_term;
var contextSysId = '2821675a5b30130070e4492c11f91a89'; // cxs_res_context_config
var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
var response = contextualSearch.search(contextSysId, searchtermStr);
var success = contextualSearch.processSearchResponse(response);
if(!success) {
vaVars.countInt = 0;
return '';
}
var searchresultsObj = response.results;
for(var i = 0; i < searchresultsObj.length; i++) {
searchresultsObj[i].count = searchresultsObj[i].meta.viewCount;
}
function rankingSorter(firstKey, secondKey) {
return function(a, b) {
if(a[firstKey] > b[firstKey]) {
return -1;
} else if(a[firstKey] < b[firstKey]) {
return 1;
} else {
if(a[secondKey] > b[secondKey]) {
return 1;
} else if (a[secondKey] < b[secondKey]) {
return -1;
} else {
return 0;
}
}
}
}
var sortedObj = searchresultsObj.sort(rankingSorter("count", "title"))
vaVars.countInt = sortedObj.length;
vaVars.resultsObj = JSON.stringify(sortedObj);
return '';
})()
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 11:01 PM
Hi there,
Nice question! Somehow didn't thought of this myself... strangely enough we did exactly setup this for our Order an Item topic. Displaying the most viewed Catalog Items first. We added this logic to the Contextual Search step. Just quering the sp_log table (for Knowledge Articles obviously a little bit different), adding the count to the var response = contextualSearch.search(context, search_term);, and than sorting the results with a function.
Will look into this later, maybe also easy to implement for knowledge, nice enhancement which I didn't think of yet.
I don't know your level of scripting, though sharing below the changed contextual search step for our order an item topic. Maybe this is already enough for you to understand the logic we've implemented on that topic.
(function execute() {
vaVars.index = 0;
var search_term = vaInputs.search_term;
var context = '6f7e3f565b30130070e4492c11f91aea'; // cxs_res_context_config
var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
var response = contextualSearch.search(context, search_term);
var success = contextualSearch.processSearchResponse(response);
if(!success) {
vaVars.countInt = 0;
return '';
}
var searchResults = response.results;
var results = [];
for (var i = 0; i < searchResults.length; i++) {
var sys_id = searchResults[i].id.split(':')[1];
var ga = new GlideAggregate('sp_log');
ga.addEncodedQuery('id=' + sys_id + '^type=Cat Item View^ORtype=Cat Item Request');
ga.addAggregate('COUNT');
ga._query();
var countInt = 0;
if(ga._next()) {
countInt = ga.getAggregate('COUNT');
}
results.push({
'sys_id': sys_id,
'title': searchResults[i].title,
'image': searchResults[i].image.link,
'count': parseInt(countInt)
});
}
function rankingSorter(firstKey, secondKey) {
return function(a, b) {
if (a[firstKey] > b[firstKey]) {
return -1;
} else if (a[firstKey] < b[firstKey]) {
return 1;
}
else {
if (a[secondKey] > b[secondKey]) {
return 1;
} else if (a[secondKey] < b[secondKey]) {
return -1;
} else {
return 0;
}
}
}
}
var sorted = results.sort(rankingSorter("count", "title"));
vaVars.countInt = results.length;
vaVars.results = JSON.stringify(sorted);
return '';
})()
Obviously it is a customization, as this is not available in the out-of-the-box topic.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2019 01:44 AM
Seemed a lot easier and quicker then I thought. Just giving you the thought + full code below.
First, I thought, let's check what the Contextual Search actually gives us. Notice the viewCount property.
So could we sort on this? Actually, meta.viewCount I don't know... though looking at the solution which we got for Order an Item (and posted in the previous post), we could add a count. So started testing this with background script (easier and quicker to test then kicking of the Virtual Agent topic everytime).
WORKS!!!
Our fullcode for the Contextual Search step:
(function execute() {
vaVars.indexInt = 0;
var searchtermStr = vaInputs.search_term;
var contextSysId = '2821675a5b30130070e4492c11f91a89'; // cxs_res_context_config
var contextualSearch = new sn_itsm_va.VAContextualSearchUtil();
var response = contextualSearch.search(contextSysId, searchtermStr);
var success = contextualSearch.processSearchResponse(response);
if(!success) {
vaVars.countInt = 0;
return '';
}
var searchresultsObj = response.results;
for(var i = 0; i < searchresultsObj.length; i++) {
searchresultsObj[i].count = searchresultsObj[i].meta.viewCount;
}
function rankingSorter(firstKey, secondKey) {
return function(a, b) {
if(a[firstKey] > b[firstKey]) {
return -1;
} else if(a[firstKey] < b[firstKey]) {
return 1;
} else {
if(a[secondKey] > b[secondKey]) {
return 1;
} else if (a[secondKey] < b[secondKey]) {
return -1;
} else {
return 0;
}
}
}
}
var sortedObj = searchresultsObj.sort(rankingSorter("count", "title"))
vaVars.countInt = sortedObj.length;
vaVars.resultsObj = JSON.stringify(sortedObj);
return '';
})()
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field