Display Multiple Catalog Items as a named link in Virtual agent

Harsha M V
Tera Contributor

Hi All,

How to display multiple link as named Catalog Item in ServiceNow Virtual agent

I have queried and got the name,sys_id,and short_description of 4 catalog items where name contains "Laptop" and have stored it in a variable.


All variables name with value display==[{"name":"Standard Laptop","sysid":"04b7e94b4f7b4200086eeed18110c7fd","short_description":"Lenovo - Carbon x1"},{"name":"Development Laptop (PC)","sysid":"3cecd2350a0a0a6a013a3a35a5e41c07","short_description":"Dell XPS 13"},{"name":"Developer Laptop (Mac)","sysid":"774906834fbb4200086eeed18110c737","short_description":"Macbook Pro"},{"name":"Sales Laptop","sysid":"e212a942c0a80165008313c59764eea1","short_description":"Acer Aspire NX"}]

 

I want my BOT response in such a way that, Standard laptop is the name of catalog and when clicked it should go to Portal link of that Catalog Item.

Standard Laptop
Lenovo - Carbon x1

Development Laptop (PC)
Dell XPS 13"},{"name":"Developer Laptop (Mac)

I know we can do this as a static one
But how to do it dynamic so that it enters the loop and gets the name,short_description and sys_id and

(function execute(header) {
    //Return up to 3 links.  For example:

       var groupedLinksOutMsg = new sn_cs.GroupedPartsOutMsg();
       groupedLinksOutMsg.setHeader(header);
       groupedLinksOutMsg.addLinkPart('link 1 label', 'www.link1.com', 'a short desc');
       groupedLinksOutMsg.addLinkPart('link 2 label', 'www.link2.com', 'a short desc');
       groupedLinksOutMsg.addLinkPart('link 3 label', 'www.link3.com', 'a short desc');
       return groupedLinksOutMsg;

})(header)

 

2 REPLIES 2

Richard Kiss1
Kilo Guru

Hi Harsha,

 

I don't really understand what you have the problem with, but I had almost the same requirement.

 

The way to dynamically set the links is just a simple loop like this. You have to construct the URL for the link as well. The script in your case would be something like this:

var groupedLinksOutMsg = new sn_cs.GroupedPartsOutMsg();

groupedLinksOutMsg.setHeader(header);

var resultsObj = [{}, ... {}];//your list of cat items

for(var i = 0; i < resultsObj.length; i++) {

var url = gs.getProperty('glide.servlet.uri') + "/sp?id=sc_cat_item&sys_id=" + resultsObj[i].sysid;

        groupedLinksOutMsg.addLinkPart(

           resultsObj[i].name,

           url,

           resultsObj[i].short_description,

           resultsObj[i].misc);       

       }

       return groupedLinksOutMsg;

Where the resultsObj is the array containing the objects with 4 properties (I don't know if you can leave out the fourth or not). the link label, the link URL, a summary text and a miscellanious text (that is the small text at the bottom). Please note that you can show only 3 items at a time, this is an out of the box limitation of the bot response output. If you want to show more than 3, you have to use a HTML bot response instead.

 

I hope this helps,

Richard

AnilM99
Tera Expert

Hi @Harsha

 

First create the script variables, pic the the 'script action utility' and load the script variables like below

 

var result = [{"name":"Standard Laptop","sysid":"04b7e94b4f7b4200086eeed18110c7fd","short_description":"Lenovo - Carbon x1"},{"name":"Development Laptop (PC)","sysid":"3cecd2350a0a0a6a013a3a35a5e41c07","short_description":"Dell XPS 13"},{"name":"Developer Laptop (Mac)","sysid":"774906834fbb4200086eeed18110c737","short_description":"Macbook Pro"},{"name":"Sales Laptop","sysid":"e212a942c0a80165008313c59764eea1","short_description":"Acer Aspire NX"}];

 

var response = JSON.parse(result);
vaVars.title1 = response[0].name;
var sysID1 = response[0].sysid;
vaVars.shortDescription1 = response[0].short_description;
vaVars.title2 = response[1].name;
var sysID2 = response[1].sysid;
vaVars.shortDescription2 = response[1].short_description;
vaVars.title3 = response[2].name;
var sysID3 = response[2].sysid;
vaVars.shortDescription3 = response[2].short_description;
 
 
vaVars.url1 = gs.getProperty('glide.servlet.uri') + "/sp?id=sc_cat_item&sys_id="sysID1;
vaVars.url2 = gs.getProperty('glide.servlet.uri') + "/sp?id=sc_cat_item&sys_id="sysID2;
vaVars.url3 = gs.getProperty('glide.servlet.uri') + "/sp?id=sc_cat_item&sys_id="sysID3;
 
then pic the link respose and add the below code
 

(function execute(header) {
       var groupedLinksOutMsg = new sn_cs.GroupedPartsOutMsg();
       groupedLinksOutMsg.setHeader(header);
       groupedLinksOutMsg.addLinkPart(vaVars.title1, vaVars.url1, vaVars.shortDescription);
       groupedLinksOutMsg.addLinkPart(vaVars.title2, vaVars.url2, vaVars.shortDescription);
       groupedLinksOutMsg.addLinkPart(vaVars.title3, vaVars.url3, vaVars.shortDescription);
       return groupedLinksOutMsg;

})(header)

 
Thanks
Anil!