Putting a counter on the list collector for Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2017 01:16 PM
Hello,
One of the requirements for a new catalog item is to get the count from the number of items moved from the list collector slush bucket to the right side. The group using this catalog item might have 100s of CIs moved to the right side and would like to get a count. What would be the best practice to show this number to the requester and how would it be done? This will be used on the Service Portal.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2017 03:52 PM
Hi Douglas,
Since every item on the right side of a list collector will be seperated by a comma, you can count the commas; then add 1 to the count to account for the last value in the list collector. After you have the count you can set that value in a count field.
I setup a test catalog item with 2 fields, a list collector, and a string field to hold the count.
Here is the code to get the count, you can put this in a client script on the item.
//I named my list collector field u_lc
var a = g_form.getValue('u_lc');
//alert(a);
//match commas and get the count,
//add 1 to account for the last comma-less value:
var b = a.match(/,/g).length + 1;
//set count field, I named my cound field u_lc_count:
g_form.setValue('u_lc_count', b);
Here I run the code with the first alert not commented-out, so you can see the commas separating the items on the right side:
There are only 5 commas, but 6 items. So I add a +1 to the comma count and populate the count string field with the total:
Have a good weekend!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2017 10:53 AM
Justin,
This works great, after moving the items to the right side and submitting the request I'm generating a number in the specified string field. Is there a way to get this field to calculate as the items are moved to the right? I've tried to make it an onChange client script with no luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2017 12:07 PM
I recommend calculating the count on form submission if possible. There are some issues with a list collector when removing items from the right side. When you get down to the last item on the right side and remove it, no onChange event occurs. This is problematic because we need the onChange to fire to update the count field!
OnChange event fires for a List Collector-
OnChange event fires when Adding:
-User clicks an item on the left side.
-User double clicks an item on the left side, or after selecting an item on the left side- the user clicks the Arrow button to move an item to the right side.
OnChange event fires when Removing:
-User clicks an item on the left side ONLY when there are more than 1 item available.
-User double clicks an item on the right side, or clicks the Arrow button to move an item back to the left side ONLY when there are more than 1 items available.
The problem occurs when there is only 1 item on the right side of a list collector. I have no idea if this is working as intended or if Service Now will update the event to fire when there is only 1 item remaining on the right side and it gets removed.