Create incident from scheduled report job

snuser10
Kilo Expert

Is there a way to create an incident ticket when report is scheduled to run? Meaning can the same scheduled job open an incident ticket with that report as an attachment? TIA!!

11 REPLIES 11

ARG645
Tera Guru

snuser10,

Using the serverSide script in the conditional field will not help. From what I see, if you want to open the incident and attach the generated report from the scheduled Report, the Report should already be generated. Conditional field script will execute before the report generation, so even though it can open an incident, it cannot attach the generated report to the incident as the report is not yet produced. 

 

Every time a schedule report runs, it creates a record in sys_email table, and your generated report will be attached in this record. 

What you can do is to write an onAfter insert Business rule on sys_email table, which is nothing but your email table. Try the below script in the business rule. 

(function executeRule(current, previous /*null when async*/) {
	
	var ScheduledReportName = "MyScheduled";//Scheduled Report Name
	/*Find if the email is generated from your scheduled report*/
	var emailTypeGr = new GlideRecord('sys_email_log');
	emailTypeGr.addQuery('email',current.getValue('sys_id'));
	emailTypeGr.query();
	if(emailTypeGr.next()){
		if(emailTypeGr.getDisplayValue('notification') == ScheduledReportName){
			/*Create the Incident */
			var incGr = new GlideRecord('incident');
			incGr.initialize();
			/*Map the required values*/
			incGr.short_description = ' ??????????';
			incGr.impact = '???';
			incGr.urgency = '???';
			incGr.assignment_group.name = '????';
			incGr.Description = '?????????';
			var incidentSysId = incGr.insert();
			
			/*Copy the attachment to the newly created incident*/
			GlideSysAttachment.copy(current.getTableName(), current.getValue('sys_id'), 'incident', incidentSysId);
			
		}
	}
	
	
})(current, previous);

 

The above script will do the following 

1. Check if the newly Generated email is produced from the scheduled Job named 'MyScheduled'

2. If yes in step 1, then create and incident 

3. Copy the generated report (which will be attached in the email record) to the newly created incident. 

 

Please mark the answer helpful/correct if applicable. 

Thank you.

ARG

Hi Aman,

 

I tried above after insert business rule with ScheduledReportName matching my scheduled report but it did not createa new incident.  Am i missing anything else?

 

(function executeRule(current, previous /*null when async*/) {
var ScheduledReportName = "Scheduled execution of Inc test";//Scheduled Report Name
/*Find if the email is generated from your scheduled report*/
var emailTypeGr = new GlideRecord('sys_email_log');
emailTypeGr.addQuery('email',current.getValue('sys_id'));
emailTypeGr.query();
if(emailTypeGr.next()){
if(emailTypeGr.getDisplayValue('notification') == ScheduledReportName){
/*Create the Incident */
var incGr = new GlideRecord('incident');
incGr.initialize();
/*Map the required values*/
incGr.short_description = 'Incident Test';
incGr.impact = '3 - Low';
incGr.urgency = '3 - Low';
incGr.assignment_group.name = 'Service Desk';
incGr.Description = 'Desc goes here';
var incidentSysId = incGr.insert();

/*Copy the attachment to the newly created incident*/
GlideSysAttachment.copy(current.getTableName(), current.getValue('sys_id'), 'incident', incidentSysId);
}
}
})(current, previous);

I realized that a scheduled report email doesn't produce an entry in the sys_email_log table. I simplified the code a bit, and i tested it in my demo instance. Please give it a shot.

(function executeRule(current, previous /*null when async*/) {
	
	var ScheduledReportName = "Scheduled execution of Inc test";//Scheduled Report Name
	/*Find if the email is generated from your scheduled report*/
	
	if(current.instance.name == ScheduledReportName){
		/*Create the Incident */
		var incGr = new GlideRecord('incident');
		incGr.initialize();
		/*Map the required values*/
		incGr.short_description = ' ??????????';
		incGr.Description = '?????????';
		var incidentSysId = incGr.insert();
		
		/*Copy the attachment to the newly created incident*/
		GlideSysAttachment.copy(current.getTableName(), current.getValue('sys_id'), 'incident', incidentSysId);
		
	}
	
	
})(current, previous);

This worked perfectly in my instance.  Thank you for this.

Welcome !!