Pin a catalog item to the list of popular items

Shannon Burns
Kilo Sage

Does anyone know an easy way to always include a particular item as one of the popular items in the service portal widget?

My boss wants a particular thing to show up there (regardless of how many times it was actually requested).

Thanks,

Shannon

1 ACCEPTED SOLUTION

Not ideal/best practice - you'll undoubtedly want to change this as some point and having to re-code the widget isn't an ideal way of updating this.   Personally and without knowing further details about your use case or environment, I'd lean towards a System Property.



But here's some quick, untested code that you can try in the Server Script of your widget to get a particular catalog item included.   Add to about line 5 of the OOB server script just after var items = [];



var sticky = new GlideAggregate('sc_cat_item');


sticky.addQuery('sys_id','[INSERT SYS ID HERE]');


sticky.query();


while (sticky.next()) {


  if (!$sp.canReadRecord("sc_cat_item", sticky.sys_id))


            continue; // user does not have permission to see this item


  var item = {};


  item.name = sticky.name.getDisplayValue();


  item.short_description = sticky.short_description.getDisplayValue();


  item.picture = sticky.picture.getDisplayValue();


  item.price = sticky.price.getDisplayValue();


  item.sys_id = sticky.sys_id.getDisplayValue();


  items.push(item)


}




EDIT: jacob.benker had a good presentation on Data Driven Development (including how to leverage System Properties for use cases like these) in Creator Con breakout session (CCB4208) at Knowledge17 just last week.


View solution in original post

14 REPLIES 14

Can't say for sure but looking at the OOTB version they push in a page variable too which is not in the script posted by @Nia McCash , I'm guessing this will help your instance route to the correct page once the pinned item is clicked.

Try the below code and let me know if it works for you 🙂

**By the way the popular items list is also presented in the catalog on the sc_category page by a different widget (SC Category Page [sc-category] ) the below logic also works in that widget too but needs to be inserted into the relevant part of the server script (I inserted into the getPopulateItems function just after the line var allowedItems = getAllowedCatalogItems();  as this is where the "items" variable which the script pushes into is declared)

var sticky = new GlideAggregate('sc_cat_item');

sticky.addQuery('sys_id','[INSERT SYS ID HERE]');

sticky.query();

while (sticky.next()) {

   if (!$sp.canReadRecord("sc_cat_item", sticky.sys_id))
             continue; // user does not have permission to see this item

   var item = {};

   item.name = sticky.name.getDisplayValue();
   item.short_description = sticky.short_description.getDisplayValue();
   item.picture = sticky.picture.getDisplayValue();
   item.price = sticky.price;
   item.sys_id = sticky.sys_id.getDisplayValue();

   //added the below line
   item.page = 'sc_cat_item';

   items.push(item);

}

. . . also if you want your item to go to the top of the list in the popular items you might want to set the "order" for item in this script for the SC Category Page widget or the "count" for the item in this script for the SC Popular Items widget as these values are set by the OOTB scripts and control the placement of the item in the list.

The OOTB subtracts the items count from zero so the items with the largest count will have the largest negative number and therefore appear first in the list.

To force your item to the top of the list either dynamically derive the MIN count and then set your items count/order value to a larger negative number (e.g. if MIN = -100 set yours to -101) or if you want a simpler solution you can just set your items count/order to a suitably massive static negative number that you can be sure you won't hit for any other item in the time period your popular items are counted for (e.g. -1000000000)

here's and example for the SC Category Widget

var sticky = new GlideAggregate('sc_cat_item');

sticky.addQuery('sys_id','[INSERT SYS ID HERE]');

sticky.query();

while (sticky.next()) {

   if (!$sp.canReadRecord("sc_cat_item", sticky.sys_id))
             continue; // user does not have permission to see this item

   var item = {};

   //added the below line
   item.order = -100000000000;

   item.name = sticky.name.getDisplayValue();
   item.short_description = sticky.short_description.getDisplayValue();
   item.picture = sticky.picture.getDisplayValue();
   item.price = sticky.price;
   item.sys_id = sticky.sys_id.getDisplayValue();

   //added the below line
   item.page = 'sc_cat_item';

   items.push(item);
}

 

here's an example for the SC Popular Items widget

var sticky = new GlideAggregate('sc_cat_item');

sticky.addQuery('sys_id','[INSERT SYS ID HERE]');

sticky.query();

while (sticky.next()) {

   if (!$sp.canReadRecord("sc_cat_item", sticky.sys_id))
             continue; // user does not have permission to see this item

   var item = {};

   //added the below line
   item.count = -100000000000;

   item.name = sticky.name.getDisplayValue();
   item.short_description = sticky.short_description.getDisplayValue();
   item.picture = sticky.picture.getDisplayValue();
   item.price = sticky.price;
   item.sys_id = sticky.sys_id.getDisplayValue();

   //added the below line
   item.page = 'sc_cat_item';

   items.push(item);
}

Hi @Francis Cavaciuti ,

Thank you for the helpful information! Related to this post, please let me ask one question.

I would like "Popular Items" section to show all the catalog items (when no category is selected in SC Category page).

Could you please advise me on how to make a customization for the requirement?

Please kindly give me some advice here or in my new Question below:

https://community.servicenow.com/community?id=community_question&sys_id=478533001b1bf850a59033f2cd4bcb93

 

Best Regards,

Aki

ashi7
Kilo Contributor

can anyone please help me. I have included external content catalog item in list of popular items. but from the list, it doesn't open the external url directly, it opens the catalog item which contains url.


Ashi,



Check your 'Target' on that Content Item. Make sure it is set to "New Window/Tab".