Adding GlideDuration Values

stevejarman
Giga Guru

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());

1 ACCEPTED SOLUTION

Steve McCarty
Mega Guru

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'));
}


View solution in original post

2 REPLIES 2

Steve McCarty
Mega Guru

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'));
}


That's really nice. Thanks! Hadn't come across the GlideAggregate previously.