attach scheduled report to task

russellprice
Tera Contributor

Hi

We currently have a scheduled task which is triggered every week to check cert expiries. We also have a scheduled report which emails the current certs which are due to expire.

I was wondering wether there was a way to get the scheduled report to automaticaly attach itself to the scheduled task so that there is no need to search for the email in your inbox?

Thanks

Russell

1 ACCEPTED SOLUTION

Again getting a report attached to a task is tricky so hopefully this solution will satisfy your requirements.  Idea is that your list of expired certs will show up as Affected CI's on this custom task.  From there you can drill into them to see details on the certificate and its "Affected by Task" list.  In my testing of this solution, I created an incident, but it doesn't matter which type of task you are creating.

 

The description field gets set by a list of certificate names:

find_real_file.png

 

The Affected CI list is populated with this same list of certificates:

find_real_file.png

 

  • Navigate to your existing scheduled job and deactivate it.
  • Navigate to System Definition\Scheduled Jobs and click New
  • Choose "Automatically run a script of your choosing"
  • Give it a name that makes sense to you
  • Set the schedule to run it
  • Paste in the following code.  I put in comments so you know whats happening to make tweaks
// Enter the SysID of the template used to create the task
var templateID = "90bc17044f751b00d1676bd18110c716";

var templateRec = new GlideRecord("sys_template");
if (templateRec.get(templateID)) {
	var taskTableName = templateRec.table;
	
	// Initialize task record and apply template
	var taskRec = new GlideRecord(taskTableName);
	taskRec.newRecord();
	GlideTemplate.get(templateID).apply(taskRec);
	
	// Get a list of expired certificates and add as Affected CI
	var certNameList = [];
	var certRec = new GlideRecord("u_cmdb_ci_certificates");
	certRec.addEncodedQuery("u_expiration_days<30^install_status=102^EQ");
	certRec.query();
	while (certRec.next()) {
		certNameList.push(certRec.getDisplayValue());
		var affectedCI = new GlideRecord("task_ci");
		affectedCI.ci_item = certRec.sys_id;
		affectedCI.task = taskRec.sys_id;
		affectedCI.insert();
	}
	
	// Set list of certificates into task description field
	var certDescription = "List of expired certificates:\n";
	certDescription = certDescription + certNameList.join(", ");
	if (gs.nil(taskRec.description)) {
		taskRec.description = certDescription;
	} else {
		taskRec.description = taskRec.description + "\n" + certDescription;
	}
	
	// Insert Task
	taskRec.insert();
	
} else {
	gs.log("Cannot create expired certificate task because no template found");
}

Please let me know if you have any questions.

View solution in original post

16 REPLIES 16

That would have been perfect! but they have just changed the goal posts on me! the analyst needs to be able to click on the certificate and support group CIs to be able to view current work notes etc. I believe the only way of doing this will be to add the URL of the report to the scheduled task?

Manik
ServiceNow Employee
ServiceNow Employee

Hi Russel,

As you are looking for a relationship between CIs and Certificate it is better to have certificate in a related list on CI which would provide access to Certificate on Task and you can populate this data using a Scheduled Job.

Thanks,

Manik 

PS - Please hit like or mark correct and helpful if it helps