Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

compare two scores or current vs previous values

mikereaves
Tera Expert

My client has a need to trend pm_project.work_effort at the record level.
I'd like to keep the solution entirely in PA rather than adding fields and/or business rules, etc.

The field: 'work_effort' is labeled: "Actual effort".
Can I use Bucket groups?

I've created a bucket group w/ three buckets
- increased (1-3)
- decreased (4-6)
- unchanged (7-9)

I've created a script: 'work effort direction'

var direction = function(curr_we, prev_we){

if (curr_we > prev_we){
    return 1;
    }
if (curr_we < prev_we){
    return 4;
    }
if (curr_we == prev_we){
    return 7;
   }
};
direction(current.work_effort, previous.work_effort);


I'd like to create an indicator "count of projects by work effort direction" with breakdown "PRJ Work Effort Direction".

The data collection job gets an error:
"Error during JavaScript evaluation com.snc.pa.dc.ScriptException: Cannot read property "work_effort" from nullTypeError: Cannot read property "work_effort" from null (<refname>; line 13) in script: var direction = function(curr_we, prev_we){...

In a PA Script, am I able to refer to "current" & "previous" ?

ANY COMMENTS GREATLY APPRECIATED@!@@@

1 REPLY 1

wojasso
Giga Guru

Hi @mikereaves

Bucket groups in Performance Analytics are designed to classify a single numeric value into discrete ranges. They don’t automatically keep track of change over time — that’s why you’re trying to calculate an increase/decrease yourself.

In a PA collection script you can reference the current and previous sample, but be aware that previous will be null on the first record for a time series or whenever there’s no earlier sample. That’s why your code fails — you are unconditionally reading previous.work_effort.

  • Add a null check so that you handle the first sample gracefully. You can also default to the current value if there is no previous record.
  • Cast the work effort values to numbers in case they are stored as strings.
  • Return one of the bucket keys (1, 4 or 7) based on the comparison.
var curr = current.work_effort || 0;
var prev = (previous && previous.work_effort) || curr;
// Convert to floats
curr = parseFloat(curr);
prev = parseFloat(prev);
if (curr > prev) {
    return 1; // increased
}
if (curr < prev) {
    return 4; // decreased
}
return 7; // unchanged

If you use this script on your indicator, your bucket group (1‑3 = increased, 4‑6 = decreased, 7‑9 = unchanged) will evaluate correctly and the data collection job will no longer throw a null error.

You can then define your indicator as a “count of projects” grouped by the custom breakdown you created.



💥 Was this answer useful? 👉 If so, click 👍 Helpful 👍 or Accept as Solution 💡🛠😀🙌