Scheduled job is not triggering event for glideaggregate

pokai
Tera Contributor

I'm new to developing to bear with me.
I am trying to do a daily scheduled job to check the number of hours worked for the current month and if it breaches 7 to trigger an event. I can get the log to come through but the event fails to trigger. Can anyone advise what I did wrong?

Error: 

My scheduled job script is below:

for(var i = 0; i< 1; i++){


var agg = new GlideAggregate('task_time_worked');


agg.addAggregate('sum','u_time_in_hours');


agg.setGroup(false);


agg.addQuery('sys_created_on', '>=', 'javascript:gs.monthsAgoStart('+i+')');


agg.addQuery('sys_created_on', '<=', 'javascript:gs.monthsAgoEnd('+i+')');


agg.query();


var count;


while (agg.next())


count = agg.getAggregate('sum','u_time_in_hours');

var limit = '7';
if(count>limit){
gs.log ('Deloitte has worked ' + count + ' hours this month');
gs.eventQueue ('deloitte.notification.hours.breached', agg, '', '');
//gs.print(count);
}
}

6 REPLIES 6

sanh
Tera Contributor
Hi, You have missed the open brace next to the while loop. Also as per your requirement I feel you are missing the group by assigned to. Per my understanding for the particular person who worked more than 7 hrs in the current month should get a notification. In this case first query with start and end date, then group by your assigned to then do a sum up then check if it is greater than 7 then send a notification. Thanks. Please like or click answered if it is helpful.

simonbergstedt
Kilo Sage

Hmm seems like agg isn't a record in the table that is specified in the event registry for "deloitte.notification.hours.breached", can you check that? Also it is probably a good idea to write agg to the log to see if you get the sys_id of a record from that table.

SanjivMeher
Mega Patron

What do you see in the logs for gs.log ('Deloitte has worked ' + count + ' hours this month');?

 

var limit = 7;
var dur = new GlideDuration(60 * 60 * limit * 1000);

 

I think you need to covert it to duration before comparing

pokai
Tera Contributor

Please see attached on what I see in the logs.

Can anyone confirm that GlideAggregate works for triggering events?

I've also added the open brace as sanh advised but still getting the same error

for(var i = 0; i< 1; i++){


var agg = new GlideAggregate('task_time_worked');


agg.addAggregate('sum','u_time_in_hours');


agg.setGroup(false);


agg.addQuery('sys_created_on', '>=', 'javascript:gs.monthsAgoStart('+i+')');


agg.addQuery('sys_created_on', '<=', 'javascript:gs.monthsAgoEnd('+i+')');


agg.query();


var count;


while (agg.next()){


count = agg.getAggregate('sum','u_time_in_hours');


}
}

//var gr = new GlideRecord('u_time_in_hours');
var limit = '7';
if(count>limit){
gs.log ('Deloitte has worked ' + count + agg + ' hours this month');
gs.eventQueue ('deloitte.notification.hours.breached', agg, '', '');
//gs.print(count);
}