
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2016 03:14 PM
Is anyone able to tell me why the following code fails? Given that time_worked is a Duration field. I just want to add all these Durations together.
I get an error:
Error running business rule '(AC3) Create Task Time Record' on incident:, exception: org.mozilla.javascript.EvaluatorException: Cannot convert 1970-01-01 00:15:00 to java.lang.Long (sys_script.0bc340a64fa11200a7d120201310c70b; line 89)
Why is it trying to convert the Duration to Long, when GlideDuration.add() supposedly takes a GlideDuration? The final line results in "undefined". Line 89 is the totalEffort.add(totalEffortRecordSet.time_worked);
var totalEffortRecordSet = new GlideRecord("task_time_worked");
totalEffortRecordSet.addQuery("task", current.sys_id);
totalEffortRecordSet.query();
var totalEffort = new GlideDuration();
while (totalEffortRecordSet.next())
{
totalEffort.add(totalEffortRecordSet.time_worked);
}
gs.log(totalEffort.dateNumericValue());
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2016 03:57 PM
I'm not sure why that doesn't work, but you might be interested in trying this. Using a GlideAggregate will let the database engine do the adding for you and speed up your script.
var totalEffortRecordSet = new GlideAggregate("task_time_worked");
totalEffortRecordSet.addQuery("task", current.sys_id);
totalEffortRecordSet.addAggregate('SUM','time_worked');
totalEffortRecordSet.groupBy('task');
totalEffortRecordSet.query();
if(totalEffortRecordSet.next())
{
gs.log(totalEffortRecordSet.getAggregate('SUM','time_worked'));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2016 03:57 PM
I'm not sure why that doesn't work, but you might be interested in trying this. Using a GlideAggregate will let the database engine do the adding for you and speed up your script.
var totalEffortRecordSet = new GlideAggregate("task_time_worked");
totalEffortRecordSet.addQuery("task", current.sys_id);
totalEffortRecordSet.addAggregate('SUM','time_worked');
totalEffortRecordSet.groupBy('task');
totalEffortRecordSet.query();
if(totalEffortRecordSet.next())
{
gs.log(totalEffortRecordSet.getAggregate('SUM','time_worked'));
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2016 04:38 PM
That's really nice. Thanks! Hadn't come across the GlideAggregate previously.