Skip to content

Commit 562f55d

Browse files
authored
Update script.js
1 parent ed18c98 commit 562f55d

File tree

1 file changed

+30
-30
lines changed
  • Modern Development/Service Portal Widgets/Catalog Item Explorer

1 file changed

+30
-30
lines changed

Modern Development/Service Portal Widgets/Catalog Item Explorer/script.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
/* Get Catalog ID */
2323
var catalogsId = $sp.getParameter("used_catalog") || options.used_catalog;
2424

25-
/* Get all catalog items */
25+
/* Get all catalog items which are active and not marked hidden on service portal */
2626
var catalogItems = new GlideRecordSecure('sc_cat_item');
2727
catalogItems.addQuery('sc_catalogs', 'IN', catalogsId);
2828
catalogItems.addQuery('active', true);
29+
catalogItems.addQuery('hide_sp', false);
2930
catalogItems.orderBy('name');
3031
catalogItems.query();
3132

@@ -51,6 +52,7 @@
5152
itemId: catalogItems.getUniqueValue(),
5253
name: catalogItems.getValue('name'),
5354
description: catalogItems.getValue('short_description'),
55+
type: catalogItems.getDisplayValue('sys_class_name'),
5456
externalUrl: extUrl
5557
});
5658
}
@@ -59,39 +61,37 @@
5961
data.catalogCategories = getUniqueFirstLetters(data.catalogItems);
6062

6163
function getUniqueFirstLetters(strings) {
62-
/* Create an empty array to store the first letters */
63-
var firstLetters = [];
64+
/* Create an object to store unique first letters */
65+
var firstLettersMap = {};
6466

65-
/* Iterate over the input array of strings */
66-
for (var i = 0; i < strings.length; i++) {
67-
/* Get the first letter of the current string */
68-
var firstLetter = strings[i].name.charAt(0);
69-
var exists = false;
67+
/* Iterate over the input array of strings */
68+
for (var i = 0; i < strings.length; i++) {
69+
/* Get the first letter of the current string and convert it to uppercase */
70+
var firstLetter = strings[i].name.charAt(0).toUpperCase();
7071

71-
/* Check if the letter already exists in the array */
72-
for (var j = 0; j < firstLetters.length; j++) {
73-
if (firstLetters[j].letter === firstLetter.toUpperCase()) {
74-
exists = true;
75-
break;
76-
}
77-
}
72+
/* Use the letter as a key in the object to ensure uniqueness */
73+
if (!firstLettersMap[firstLetter]) {
74+
firstLettersMap[firstLetter] = true;
75+
}
76+
}
7877

79-
/* Check if the first letter already exist in the array */
80-
if (!exists) {
81-
/* If not add it */
82-
firstLetters.push({
83-
letter: firstLetter,
84-
selected: false
85-
});
86-
}
87-
}
78+
/* Convert the object keys to an array of objects */
79+
var firstLetters = [];
80+
for (var letter in firstLettersMap) {
81+
if (firstLettersMap.hasOwnProperty(letter)) {
82+
firstLetters.push({
83+
letter: letter,
84+
selected: false
85+
});
86+
}
87+
}
8888

89-
/* Sort the array of objects, otherwise the simplier version of sort might be used */
90-
firstLetters.sort(function (a, b) {
91-
return a.letter.localeCompare(b.letter);
92-
});
89+
/* Sort the array of objects */
90+
firstLetters.sort(function (a, b) {
91+
return a.letter.localeCompare(b.letter);
92+
});
9393

94-
/* Return the sorted array of unique first letters */
95-
return firstLetters;
94+
/* Return the sorted array of unique first letters */
95+
return firstLetters;
9696
}
9797
})();

0 commit comments

Comments
 (0)