Virtual agent, limiting KB search results

Jonathan28
Giga Contributor
Hi
I would like to limit the amount of KB search results that I get in the Virtual agent.
I have been trying to play around with the ".setlimit()" function but haven't been able to solve the problem
 
Could someone put me in the direction in what I need to do to in order to fix this?
 
 
Below is the code for displaying kb articles taken from the "Submit an incident" topic.
 
(function execute() {
var kbs = JSON.parse(vaVars.search_kb_json_string);
var helper = new ItsmVaHelper();
var baseURL = gs.getProperty('glide.servlet.uri') + '/' + gs.getProperty('sn_itsm_va.com.snc.itsm.virtualagent.portal_url');

var items = kbs.map(function(kb) {
return helper.formatKBSearchResult(baseURL, kb);
}).join('<br />');
 
var html =
'<div>' +
'<div>' + gs.getMessage('Before opening your incident, here is some information that might help you:') + '</div><br />' + items +
'</div>';
 
return html;
}
1 ACCEPTED SOLUTION

Tudor
Tera Guru

Hi Jonathan,

 

 Please take a look at the contextual search and limit the number of articles from there:

find_real_file.png

 

in the for:

for (var index=0;index<relevantSearchResults.length;index++)
{
if (relevantSearchResults[index].meta.knowledgeBaseId=='247d3944db8ae7006bf69334ca9619ca')
{
results.push(relevantSearchResults[index]);
str = JSON.stringify( results )
vaVars.kbResultCount++;
}
}
and an if condition like:
 
for (var index=0;index<relevantSearchResults.length;index++)
{
if (relevantSearchResults[index].meta.knowledgeBaseId=='247d3944db8ae7006bf69334ca9619ca')
{
results.push(relevantSearchResults[index]);
str = JSON.stringify( results )
vaVars.kbResultCount++;
}
if (index==5) // here I set the limit to 5 ... feel free to play around
break; //will stop the loop
}

View solution in original post

8 REPLIES 8

If you are using the out-of-the-box topic or similar, in that case, have a look at the Script Variable "limit":

find_real_file.png

The limit is actually applied on the User Input "KB Selection".

find_real_file.png

The script for this contains vaVars.limit:

(function execute() {
    /*
    Pagination buttons for suggested KB articles.
    */
    var options = [];
    var limit = parseInt(vaVars.limit);
    var index = parseInt(vaVars.index);
    
    if (index >= limit) {
        options.push({ 'value': 'show_prev', 'label': gs.getMessage('Show previous')});    
    }

    if (index+limit < vaVars.kbResultCount) {
        options.push({ 'value': 'show_next', 'label': gs.getMessage('Show more')});    
    }
    options.push({ 'value': 'new_search', 'label': gs.getMessage('Try a different search')});
    options.push({ 'value': 'cancel', 'label': gs.getMessage('I\'m done')});
    return options;
})()

 

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

LinkedIn

Jonathan28
Giga Contributor

Thank you both, now the problem is solved

Hi there,

Did this solve your question? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

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

LinkedIn

Arpitha Gopalsw
Giga Guru

Hi Jonathan,

Was the OOB Contextual Search topic block was used for this? Could you kindly provide the actual steps followed to display 5 KB articles in VA. We tried the above solution. However, its not working as expected.

Regards,

Arpitha G