How to rearrange items in the Service Portal Popular Items?

yltsai
Mega Guru

To all experts,

At this moment, many of you could be in K17. But when you get a breather, please help me with the following issue.

The client wants to rearrange the order in the Service Portal Popular Items.

I cloned the widget and modified the server code but it did not get the correct order. Please help and tell me what I missed.

Server Script:

data.sc_catalog = $sp.getValue('sc_catalog');

data.showPrices = $sp.showCatalogPrices();

var items = [];

var count = new GlideAggregate('sc_req_item');

//var count = new GlideRecord('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', data.sc_catalog);

//count.orderByAggregate('COUNT', 'cat_item');

count.orderBy('cat_item.order');

count.query();

while (count.next() && items.length < 9) {

  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 = 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.sys_id = count.cat_item.sys_id.getDisplayValue();

  items.push(item);

}

data.items = items;

1 ACCEPTED SOLUTION

yltsai
Mega Guru

The client did not want to show the Popular items, so the widget instance was removed. The client accepted the change.


View solution in original post

12 REPLIES 12

Patrick Fedigan
Giga Guru

Hello,



In your array of items[ ]; you could display them in however order you want by using a specific number like so.



items[3];


items[4];


items[1];


items[0];


items[6];


etc..




Printing in a standard for loop will display in whatever order the server chooses.


Patrick,



Did you suggest to hard-coded assign the items array?


Would it be better to write a code as count.orderBy('cat_item.order');



By the way, I modified the OOB code and the items was defined as a list.



Regards,


I agree that it would be better to use orderBy. Can you change the order at all? Have you tried something like gr.orderByDesc('sys_created_on');


It was in my script to use orderBy but it does not work. Therefore, I have to ask for help from all Service Portal experts.



Regards,