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 Laurie



The widget is coded to only show a category if it contains at least one catalog item. So it would not work to have parent categories, that do no have its own items.



But what you can do is create a dummy item, and set the "available for" so nobody can see it - then the category will show in the widget


Laurie Marlowe1
Kilo Sage

Hi Lars,



I tried setting the "available for" to the admin role, however, that did not work.   After setting "available for" to admin, I impersonated a non-admin person, and they were still able to see the dummy item.   I am unable to set the role on the dummy item to "nobody".   I also tried setting "not available for" to "public", and that didn't work either.



Any ideas?



Thanks,



Laurie