Service Portal Widget

petterbj_rgmo
Tera Contributor

Hello Experts,


I have gotten a request from a customer and I do not know where to begin. On the Service Portal we have a widget that display all categories for the sc_category.parent = 'Order something'.
The customer wants a dropdown function for each category that will show all subcategories.Let me add this picture for reference:
find_real_file.png

As you can see we have added a additional level of categorization on the item that we want to display. I have copied and modified the OOTB widget called SC Categories for further configuration. Does anyone know how I best can display all of the categories in a dropdown? The serverside script is the issue here, not the HTML. Adding Server Side Script for reference:

// populate the 'data' object

data.categories = [];

var sc = new GlideRecord('sc_category');

sc.addQuery('sys_class_name', 'sc_category');

sc.addQuery('parent', 'a44ca4990fc8ea00548d85ace1050efb');

sc.addActiveQuery();

sc.orderBy('title');

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

if (data.sc_catalog)

  sc.addQuery('sc_catalog', data.sc_catalog);

sc.query();

while (sc.next()) {

  if (!$sp.canReadRecord("sc_category", sc.getUniqueValue()))

  continue;

  if (!options.check_can_view) {

  // use GlideAggregate by way of GlideRecordCounter, doesn't check canView on each item

  var count = new GlideRecordCounter('sc_cat_item_category');

  prepQuery(count);

  var item_count = count.getCount();

  if (item_count > 0) {

  var cat = {};

  cat.title = sc.title.getDisplayValue();

  cat.sys_id = sc.getUniqueValue();

  cat.count = item_count;

  cat.parent = sc.parent.getDisplayValue();

  data.categories.push(cat);

  }

  }

  if (options.check_can_view) {

  // use GlideRecord, checking canView on each item

  var itemCat = new GlideRecord('sc_cat_item_category');

  prepQuery(itemCat);

  itemCat.query();

  var validatedCount = 0;

  var checked = 0;

  while (itemCat.next()) {

  checked++;

  if ($sp.canReadRecord("sc_cat_item", itemCat.sc_cat_item))

  validatedCount++;

  // if user can't see the first 50 items in this category, give up

  if (validatedCount == 0 && checked == 50)

  break;

  // if omitting badges, and if we found one, work is done

  if (validatedCount > 0 && options.omit_badges)

  break;

  }

  if (validatedCount > 0) {

  var cat = {};

  cat.title = sc.title.getDisplayValue();

  cat.sys_id = sc.getUniqueValue();

  cat.count = validatedCount;

  cat.parent = sc.parent.getDisplayValue();

  data.categories.push(cat);

  }

  }

}

function prepQuery(gr) {

  gr.addQuery('sc_category', sc.getUniqueValue());

  gr.addQuery('sc_cat_item.active', true);

  gr.addQuery('sc_cat_item.visible_standalone', true);

  gr.addQuery('sc_cat_item.sys_class_name', 'NOT IN', 'sc_cat_item_wizard,sc_cat_item_content');

}

Does anyone know how I can modify the code so I can store and then display the subcategories?

1 ACCEPTED SOLUTION

Its a bit much code to include here, so I have attached the XML for you to import instead.


The widget calls an angular provider, which is related to the widget. Hence the three files.


View solution in original post

6 REPLIES 6

larstange
Mega Sage

Hi Petter



In Helsinki patch 4, the category widget has been updated to do this out of the box- Just click the collapsed folder icon, to fold the tree out



find_real_file.png


Hi Lars,



Thank you for your reply. Would you mind sending me a screenshot of the widget code?


Its a bit much code to include here, so I have attached the XML for you to import instead.


The widget calls an angular provider, which is related to the widget. Hence the three files.


Hi Lars,



Any idea why newly created categories do not display on the Service Portal?   I created a new category called Stuff, and it has a couple sub-categories.   The sub-categories contain items.  



I went into the Service Portal and refreshed the page.   The new category does not appear.



I went back to the parent category "Stuff", and added one item (the two sub-categories are still there).   I went into the Service Portal and refreshed the page.   The new category APPEARS, along with the new folder icon.



Any ideas?



Thanks,



Laurie