PA Script - score_end not defined/null
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 07:53 AM - edited 11-29-2024 08:01 AM
Hello, guys,
I am building a simple PA Script to calculate the average Incident Age, similar to the pa script "Incident.Age.Hours" for example. In my case, I need to calculate the age in "business time". So that's what I've built so far:
var minutes = function (startDateTime, endDateTime) {
try {
var startGDT = new GlideDateTime(String(startDateTime));
var endGDT = new GlideDateTime(String(endDateTime));
var scheduleId = gs.getProperty('<<<schedule property name>>>');
if (!scheduleId) {
throw new Error("Schedule ID property is not defined.");
}
var schedule = new GlideSchedule(scheduleId);
var timezone = schedule.getTimeZone();
if (!timezone) {
throw new Error("Timezone for the schedule is null or undefined.");
}
var duration = schedule.duration(startGDT, endGDT, timezone);
if (!duration) {
throw new Error("Failed to calculate duration.");
}
var durationInMilliseconds = duration.getNumericValue();
var calculatedMinutes = durationInMilliseconds / (1000 * 60);
return calculatedMinutes;
} catch (error) {
var errorMessage = error.message || "Unknown error occurred";
gs.error('>>> PA SCRIPT "Incident.Age.Business_Time.Minutes" ERROR: ' + errorMessage + '\nFull error:\n\n' + JSON.stringify(error, null, 2));
return null;
}
};
minutes(current.opened_at, score_end);
However, there are series of errors that I encounter:
> Error during JavaScript evaluation com.snc.pa.dc.ScriptException: null in script
> Error during JavaScript evaluation com.snc.pa.dc.ScriptException: Illegal access to getter method getMessage in class java.lang.NullPointerException in script
After some debugging turning on/off stuff, commenting out code and so on, it turned out that the issue is caused when I pass "score_end" variable. If I instead pass "new GlideDateTime" for example it works perfectly... any idea why that would be?
P.S. the PA Collector job is running on demand, only to collect data for my indicator, nothing special. The issue is not from Indicator or Job Collector config. I am sure of this.
Thank you in advance!