Scheduled job runscript add GlideAggregate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 06:45 AM - edited 08-30-2023 06:52 AM
Hi Team,
I have a requirement to send notification to manger if user created incidents greater than 5.
My code is below:
var ga = new GlideAggregate("incident");
ga.addActiveQuery();
ga.addAggregate("COUNT");
ga.groupBy("caller_id");
ga.query();
while (ga.next()){
if(ga.getAggregate("COUNT") >= 5){
gs.eventQueue("caller.reminder", ga, "", "");
}
}
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 07:19 AM
Hello @AnilM99,
Your code looks good, but you need to do a few more things to achieve your requirement:
- You need to create an event registry record for the caller.reminder event. You can do this by navigating to System Policy > Events > Registry and clicking New. You need to specify the name, description, and other details of the event.
- You need to create a notification record for the caller.reminder event. You can do this by navigating to System Policy > Notifications and clicking New. You need to specify the name, condition, recipients, message, and other details of the notification. For the recipients, you can use the caller_id.manager field to send the notification to the manager of the caller.
- You need to schedule a script or a business rule to run your code periodically. You can do this by using the Script Execution module or the Scheduled Script Execution module. You need to specify the name, script, schedule, and other details of the execution.
Hope this helps.
Kind Regards,
Swarnadeep Nandy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 07:30 AM - edited 08-30-2023 07:41 AM
Hi @AnilM99 Please find the below script which needs to be used in your scheduled job for your use case
var incidentQuery = active=true;
// Get the incident count
var incidentCount = new GlideAggregate('incident');
incidentCount.addEncodedQuery(incidentQuery);
incidentCount.addAggregate('COUNT');
incidentCount.groupBy("caller_id");
incidentCount.query();
var totalCount = 0;
while(incidentCount.next()) {
totalCount = incidentCount.getAggregate('COUNT');
// Check if the incident count is greater than 5
if (totalCount > 5) {
// Create a notification
gs.info(incidentCount.caller_id.name);
gs.info(incidentCount.caller_id.manager.name);
gs.eventQueue('caller.reminder', current, incidentCount.caller_id, incidentCount.caller_id.manager);
}
}
Thanks & Regards,
Eswar Chappa
Mark my answer correct and Helpful if this helps you 😀