Service Portal Client Script - how to get Item Category

Dale Hynes
Kilo Sage

Hi Everyone, I'm looking for assistance with Catalog Client Scripts on Service Portal. I have a variable set that is present on most items across 4 categories. However, I want to hide 1 of the fields from that variable set if the Item Category is 'Services'.

As Item Category is not a variable, I cannot use Catalog UI Policies. I can see the $scope.data for SC Catalog items and .category.name is the value I'm trying to read in a client script to trigger a     g_form.setVisible('myFieldName', false).

So far however, I've had no luck. Has anybody got this basic level of control to work in Service Portal ? (or have an alternative method ?)

1 ACCEPTED SOLUTION

HV1
Mega Guru

You cannot directly get .category.name since its not a field on catalog item table.   Catalog item to Category is an one-to-many mapping stored in 'sc_cat_item_category' table.   Thus for your requirement, you will have to get the current catalog item sys_id, then query the mapping table to see if current item falls in Category 'Services'.   If yes, then hide the variable, else not.



- Hardik Vora


View solution in original post

2 REPLIES 2

HV1
Mega Guru

You cannot directly get .category.name since its not a field on catalog item table.   Catalog item to Category is an one-to-many mapping stored in 'sc_cat_item_category' table.   Thus for your requirement, you will have to get the current catalog item sys_id, then query the mapping table to see if current item falls in Category 'Services'.   If yes, then hide the variable, else not.



- Hardik Vora


Pashya1
Tera Contributor

Hi,

We can find current catalog item sys id and category sys id from the URL with the below script.

URL :-https://instance_name/sp?id=sc_cat_item&sys_id=a0adf038137394102c5e276da64444a5&sysparm_category=a23233333d6a4142c5e206ea674410d&catalog_id=-1

var urlT = this.location.href;
var t = new URLSearchParams(urlT);
//var id = t.get('id');
var sysID = t.get('sys_id');
var catID = t.get('sysparm_category');
//console.log(product)
alert('SysID : ' + sysID + '\ncatID : ' + catID);

We will get a category ID. Now you can add your logic below, to change the behavior of form on the basis of the current category.

Please hit the 'Helpful' if it helps you in any way. Thank you!