Automatically create Incident if the count of a report is higher than 50

MT247
Tera Expert

Hi,

today we had an issue with our platform, where the mails in the outbox queue have not been processed for 4 hours. I have created a dashboard to monitor the queue, but it would be great, if i could send out a mail, or even better create an incident for the case that the count of tickets in the outbox is above 50. 

The dashboard is using a simple report on the sys_email table counting the mails in the outbox, so the number is displayed on the dashboard.

Anyone any idea on how to implement something like this?

Cheers

Michael

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Try something with a before insert BR on Email table:

var str = "mailbox=outbox^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)"; //this checks outbox emails created today
var gr = new GlideRecord('sys_email');
gr.addEncodedQuery(str);
gr.query();
var count = gr.getRowCount();
if(count>50){
gs.eventQueue(''); //Trigger notification as required
var inc = new GlideRecord('incident');
inc.initialize();
inc.your_fields = '';
inc.insert();
}

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GS-eventQueue_S_O_S_S_S

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideRecord-initialize

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

9 REPLIES 9

Prateek kumar
Mega Sage

Try something with a before insert BR on Email table:

var str = "mailbox=outbox^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)"; //this checks outbox emails created today
var gr = new GlideRecord('sys_email');
gr.addEncodedQuery(str);
gr.query();
var count = gr.getRowCount();
if(count>50){
gs.eventQueue(''); //Trigger notification as required
var inc = new GlideRecord('incident');
inc.initialize();
inc.your_fields = '';
inc.insert();
}

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GS-eventQueue_S_O_S_S_S

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GlideRecord-initialize

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hi, this will create too many notifications or tickets, once we have reached the threshold.

So basically each new mail which comes in, would create a new ticket or notification.

In addition to this creating emails for each email queued over 50 (which could be a lot of emails when a problem exists), you should use a GlideAggregate instead of GlideRecord.

MT247
Tera Expert

i have created a scheduled job containing the script for the check and the incident creation.

(function executeRule(current, previous /*null when async*/) {

var str = "mailbox=outbox^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)"; //this checks outbox emails created today
var gr = new GlideRecord('sys_email');
gr.addEncodedQuery(str);
gr.query();
var count = gr.getRowCount();
if(count>50){
gs.eventQueue(''); //Trigger notification as required
var inc = new GlideRecord('incident');
inc.initialize();
    inc.impact = '2 - Medium';
    inc.urgency = '2 - Medium';
    inc.caller_id = '69062297db4ba70057a46dda4b961984';
    inc.opened_by = '69062297db4ba70057a46dda4b961984';
    inc.category = 'Enabling Service';
    inc.subcategory = 'ServiceNow ITSM';
    inc.u_2nd_subcategory = 'Incident Management';
    inc.business_service = '493afad54f9b2a005ff82c518110c78e';
    inc.service_offering = 'a61bb2194f9b2a005ff82c518110c73f';
    inc.assignment_group = '729e40504fd0f640e3faa4eab110c73f';
    inc.contact_type = 'Monitoring';
    inc.watch_list ="fc8b6ca2371a31005248532e53990e94";
    inc.short_description = 'Monitoring Alert - Outbox is currently holding 50+ Mails, please check';
    inc.description = 'Dear ServiceNow Admins, the outbox queue is currently holding more than 50 mails and the queue is ramping up. Please check';
    inc.insert();
}

})(current, previous);

Even the Schedule job would do the same right.

Try to create an incident in 50 count increments till the threshold reaches 300.

If there are more than 300 mails in your outbox, there is really something wrong with the instance. You better open a HI ticket then.

Please mark my response as correct answer and helpful if it helped solved your question.

-Best Regards

Prateek kumar


Please mark my response as correct and helpful if it helped solved your question.
-Thanks