Bucket group and breakdown mapping via scripting

Timo Rasilainen
Kilo Explorer

Hi, I created a bucket group to classify users based on their email-addresses (User ID -field of sys_user table). I also created breakdown source using the newly created bucket group. Then I created breakdown for it, and set the mapping of it to use a script. Script runs through but not like I expect: All items go to last bucket, although they should split to all buckets.

Script is like:

var hr=function(x){
var rvalue=0;
if (x.includes("abc.com")) {
 rvalue=0; }
else if (x.includes("xyz.com")) {
 rvalue=1; }
else {
 rvalue=2;}
return rvalue;
};
hr(current);

I have set source to be sys_user and field= User ID. I assume that the problem is in parameter "current", which may not be a string value.

Can someone help me with this? Thanks in advance!

 

 

 

5 REPLIES 5

Adam Stout
ServiceNow Employee
ServiceNow Employee

1) I highly discourage use of buckets like this.  If you need to categorize the users, add a field that does it on sys_user.  The logic only runs when an email address changes and can be used in reporting and interactive filters as well.  See this blog: https://community.servicenow.com/community?id=community_blog&sys_id=2f99990fdbee5b00fece0b55ca9619fb

2) To make this work, pass a view (not just current):

var hr=function(x){
var rvalue=0;
if (x.includes("abc.com")) {
 rvalue=0; } 
else if (x.includes("xyz.com")) { 
 rvalue=1; } 
else {
 rvalue=2;}
return rvalue;
};
hr(current.email);

Timo Rasilainen
Kilo Explorer

Thanks, I hear what you are saying. For performance reasons that would be definately better. Then again, adding "reporting fields" would require system development and I was just testing some PA reporting. I would at the same time like to have as little fields as possible 🙂

Btw. the correction you suggested did not work, but anyhow I can live without this breakdown as it was just one of them.

Br, Timo

1) We can't be afraid of development.  Whether you do it or someone else does it, it needs to get done and why would we not want to do it where it can be leveraged by everyone in the process?  For me using it in reporting and interactive filters is motivation enough, but there are many other uses.  To get the most value out of the NOW platform, you have to use the platform not just restrict yourself to one feature.

2) For the script, without seeing the error, I can't identify what is wrong.  Put in some gs.info messages (like gs.info(current.email);) to see what values you are really getting.

sanddie
Kilo Explorer

I am trying to do a similar thing with bucket lists and hoped someone could be of assistance.   Keep in mind that our instance in ServiceNow is highly customized, so while we have some OTB scripts, breakdowns and such, I may have to modify some of the parameters where needed.  Here's the situation;

I have a PA to show incident backlog and created buckets that can distribute the tickets based on the range of days they have been open.  These ranges are 1-3 days, 4-5 days, 6-10 days, 11-30 days, and then Over 30-days. 

I have a breakdown source using the following script that was already in our baseline system.  The data is being collected but all under the 1-3 range bucket. 

My thought is that I need to test within the script in order to have the days the incident has been open distributed to the appropriate bucket and that is the piece that is missing.  Can anyone assist with this? 

Here is the script I'm using.  Just think I have to add the test to distribute to the buckets ranges?  Is it this simple?

var diff = function(x, y) {
return y.dateNumericValue() - x.dateNumericValue();
};
var days = function(x, y) {
return diff(x, y) / (24 * 60 * 60 * 1000);
};
days(current.opened_at, score_end);

Thanks so much.