Widgets Explanation

Sirri
Tera Guru

Hi All,

 

Below are two widgets with the server script please clearly what is exact out put from this widgets.

1)Popular Item

data.sc_catalog = $sp.getValue('sc_catalogs') || $sp.getValue('sc_catalog');
data.showPrices = $sp.showCatalogPrices();
data.limit = options.limit || 4;
var items = [];
var item_sysid = gs.getProperty('RCO.popular_item');
var count = new GlideAggregate('sc_req_item');
count.addAggregate('COUNT','cat_item');
count.groupBy('cat_item');
count.addQuery('cat_item.sys_class_name', 'NOT IN', 'sc_cat_item_guide,sc_cat_item_wizard,sc_cat_item_content');
count.addQuery('cat_item.sc_catalogs', 'IN', data.sc_catalog);
count.addQuery('cat_item.visible_standalone','true');
count.addQuery('cat_item','!=',item_sysid);//Should not display 'Can't find the right Request?' in Popular requests
count.addEncodedQuery('cat_item.hide_sp=false^ORcat_item.hide_spISEMPTY');
count.orderByAggregate('COUNT', 'cat_item');
count.query();
while (count.next() && items.length < data.limit) {
  if (!$sp.canReadRecord("sc_cat_item", count.cat_item.sys_id.getDisplayValue()))
    continue; // user does not have permission to see this item

  var item = {};
  item.count = 0 - count.getAggregate('COUNT', 'cat_item');
  item.name = count.cat_item.name.getDisplayValue();
  item.short_description = count.cat_item.short_description.getDisplayValue();
  item.picture = count.cat_item.picture.getDisplayValue();
  item.price = count.cat_item.price.getDisplayValue();
  item.hasPrice = count.cat_item.price != 0;
  item.sys_id = count.cat_item.sys_id.getDisplayValue();
  items.push(item);
}

if (options.include_record_producers == 'true' || options.include_record_producers == true) {
    var producers = 0;
    count = new GlideAggregate('sc_item_produced_record');
    count.addQuery('producer.sc_catalogs', 'IN', data.sc_catalog);
    count.addQuery('producer.visible_standalone','true');
    count.addEncodedQuery('producer.hide_sp=false^ORproducer.hide_spISEMPTY');
    count.addAggregate('COUNT', 'producer');
    count.groupBy('producer');
    count.orderByAggregate('COUNT', 'producer');
    count.query();
    while (count.next() && producers < data.limit) {
       
        var catalogItemJS = new sn_sc.CatItem(count.getValue('producer'));
        if (!catalogItemJS.canView(gs.isMobile()) || !catalogItemJS.isVisibleServicePortal())
            continue;
        var catItemDetails = catalogItemJS.getItemSummary();

        var item = {};
        item.count = 0 - count.getAggregate('COUNT', 'producer');
        item.name = catItemDetails.name;
        item.short_description = catItemDetails.short_description;
        item.picture = catItemDetails.picture;
        item.price = catItemDetails.price;
        item.hasPrice = item.price != 0;
        item.sys_id = catItemDetails.sys_id;
        item.page = 'sc_cat_item';
        items.push(item);
        producers++;
    }
}

data.items = items;
 
 
2)KB Most Viewed
 
(function() {
    data.articles = [];
    options.title = options.title || gs.getMessage("Most Viewed Articles");
   
    var z;
    var knowledge_bases;
    var sys_id = $sp.getParameter('sys_id');
    var kb_id = $sp.getParameter('kb_id');
   
    if(sys_id != null ){  /* On the Article page, get KB of the article */
        var getKB = new GlideRecord("kb_knowledge");
        if(getKB.get($sp.getParameter('sys_id'))){
            data.knowledge_base_id = getKB.getValue('kb_knowledge_base');
            knowledge_bases = [data.knowledge_base_id];
        } else  /* return false for an invalid sys_id in URL */
             return false;  
    } else{ /* For all other pages this code will get executed. Used on knowledge home page.*/
       
        /*If KB is selected display most viewed articles only from that KB*/
        if(kb_id != null)
            knowledge_bases = String(kb_id);
        else {
             /*Get all knowledge bases associated with Portal*/
            knowledge_bases = String($sp.getKnowledgeBases());
        }
    }
   
    if (GlideStringUtil.notNil(knowledge_bases))
        z = $sp.getAllKBRecords(knowledge_bases);
    else //If there are no accessible KBs for logged in user
        return;
   
    z.addQuery("sys_view_count", ">", "0");
    if (options.kb_category)
        z.addQuery("kb_category", options.kb_category);
    z.orderByDesc('sys_view_count');
    z.setLimit(options.max_number || 5);
    z.query();
    while (z.next()) {
        if (!z.canRead())
            continue;

        var a = {};
        $sp.getRecordValues(a, z, 'short_description,sys_view_count,sys_id,published');
        data.articles.push(a);
    }
})();
 
Thank you
1 REPLY 1

Anand Kumar P
Giga Patron
Giga Patron

Hi @Sirri ,

1. Popular Item Widget:

Displays the most-requested only 4 catalog items , excluding specific item types, with their name, short description, image, and price . Refer below link for more info.

 

https://www.servicenow.com/docs/bundle/xanadu-platform-user-interface/page/build/service-portal/conc...

2. KB Most Viewed Widget:

Lists most-viewed only 5 knowledge articles from the relevant knowledge bases, showing their short description and view count.

 

https://www.servicenow.com/docs/bundle/xanadu-platform-user-interface/page/build/service-portal/conc...

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand