Sagar Pagar
Tera Patron

Introduction:

I have read some questions or queries on scheduling reports for dynamic recipients based on the table column of the report. Also, I had a few issues while working on the same requirements. Hence, I have started to write this article. I think it will help community users to work on this type of requirement in the future.

Create a scheduled report which will trigger based on conditions and send it to the dynamic receipts. Dynamic recipients may be assigned to users, the assignee’s manager, etc.

Use cases:

  1. Create a scheduled report which will trigger based on some conditions and send it to dynamically assigned to users.
  2. Create a scheduled report which will trigger with conditions and send the incidents report to Managers of the assignment group.
  3. Create a scheduled report to send to the assigned_to users and his/her managers for pending approval of requests.

Procedure:

Need to follow the following procedure.

Step 1: Create a report and add the “assigned_to” or “assigned_to.manager” columns in the report.

 

Example- Create a report for example- incidents opened last week with assigned columns in it.

find_real_file.png

 

Step 2: Scheduled the same report run as on-demand.

find_real_file.png

 

Step 3: Create a scheduled script to update the dynamic assigned to the list.

find_real_file.png

 

Here is the script:


updateDynamicRecipients();

function updateDynamicRecipients(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheduled report
	scheduleReport.get("sys_id of report"); //Sys ID of your schedule Report

	var recipients = [];
	var tablename = scheduleReport.report.table;
	var query = scheduleReport.report.filter;

	var gr = new GlideRecord(tablename);
	gr.addEncodedQuery(query);
	gr.query();
	while (gr.next()) {
		recipients.push(gr.assigned_to.sys_id.toString());  // push the assigned _to users in recipients array
	}
	// gs.log(recipients.toString());
	// gs.log(recipientsManager.toString());

	var arrayUtil = new ArrayUtil();
	finalRecipients = arrayUtil.unique(recipients);   // unique elements

	scheduleReport.user_list = finalRecipients.join(',');
	//	gs.log("User list: " +current.user_list);
	scheduleReport.update();

	SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

}

 

Step 4: If need to include assigned _to Manager or assignment groups manager, then use the script.

 


updateUsersListWithManager();

function updateUsersListWithManager(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheuled report
	scheduleReport.get("42f87abc2f37201002454ae72799b66f"); //Sys ID of your schedule Report

	var recipients = []; var recipientsManager = [];
	var tablename = scheduleReport.report.table;
	var query = scheduleReport.report.filter;

	var reportgr = new GlideRecord(tablename);
	reportgr.addEncodedQuery(query);
	reportgr.query();
	while (reportgr.next()) {
		recipients.push(reportgr.assigned_to.sys_id.toString());  // push the assigned _to users in recipients array
		recipientsManager.push(reportgr.assigned_to.manager.sys_id.toString());  // push the assigned _to managers in recipientsManager array
	}
	// 	gs.log("recipients array: " + recipients.toString());
	// 	gs.log("recipients manager array: " + recipientsManager.toString());

	var arrayUtil = new ArrayUtil();
	arrayUtil.concat(recipients, recipientsManager);
	var finalRecipients = arrayUtil.unique(recipients);   // unique elements

	// 	gs.log("finalRecipients: " + finalRecipients);

	scheduleReport.user_list = finalRecipients.join(',');

	// 	gs.log("User list: " + scheduleReport.user_list);

	scheduleReport.update();

	SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

}

 

Step 5: For earlier versions of Orlando, the current object is working in the scheduled job conditional script. we can directly use the below script in the conditional option to dynamically update the recipients.

 

updateDynamicRecipients();


function updateDynamicRecipients(){

	var recipients = [];

	// 	gs.log("current.report.filter:"+ current.report.table);
	// 	gs.log("current.report.filter:"+ current.report.filter);

	var scheduledGlideRecord = new GlideRecord(current.report.table);
	scheduledGlideRecord.addEncodedQuery(current.report.filter);
	scheduledGlideRecord.query();
	while (taskQueryGR.next()){
		recipients.push(scheduledGlideRecord.assigned_to + '' );

	}

	// gs.log(recipients.toString());

	var arrayUtil = new ArrayUtil();

	var finaRrecipients = arrayUtil.unique(recipients);   // unique assigned to

	current.user_list = finaRrecipients.join(',');
	gs.log("User list: " +current.user_list);

	current.setWorkflow(false);
	current.update();
	current.setWorkflow(true);
	if (senderArray.length > 0){
		return true;
	}

	return false;

 

It will update the user's list dynamically and send the reports to recipients.

find_real_file.png

Check the mail logs.

 

In addition, to that, we can use the following methods of mail script for reports. We can specify copied and blind copied recipients by using the email object within a mail scripts.

email.setFrom("Servicedesk <servicedesk@testmail.com>");  // to set the setFrom mail address

email.setSubject("Subject of the report");  // to set the mail Subject for notification

email.addAddress("cc","sagar.pagar@testmail.com","Sagar Pagar"); // to add CC users in notification

email.addAddress("bcc","sagar.pagar@testmail.com","Sagar Pagar");  // to add BCC users in notification

 

Conclusion:

In this way, we can schedule a report with dynamic recipients.

Please provide your inputs and give suggestions if any. I would also like to hear a few use cases which I can build for you.

 

Feel free to mark helpful and bookmark this article.

Thanks,
Sagar Pagar

Comments
Alex199
Tera Explorer

Hi @Sagar Pagar , do you mind having a look at my question (just 3 posts up from the bottom)?

 

Thanks!

kpchrisman
Tera Explorer

Thanks. Problem resolved. The case can be closed now.

Joshuu
Kilo Sage

Hi @Sagar Pagar ,

 

Thank you for the article.

 

I have followed the steps which you have shared in the article to send the report to the assignment group's manager. And it is working good. Used the below script.

updateUsersListWithManager();
 
function updateUsersListWithManager() {
    var scheduleReport = new GlideRecord('sysauto_report'); // glide the Scheuled report
    scheduleReport.get("c4630ea21b394e144ff998e32a4bcb3f"); //Sys ID of your schedule Report
 
    var recipients = [];
    var recipientsManager = [];
    var tablename = scheduleReport.report.table;
    var query = scheduleReport.report.filter;
 
    var reportgr = new GlideRecord('incident');
    reportgr.addEncodedQuery(query);
    reportgr.query();
    while (reportgr.next()) {
        //recipients.push(reportgr.assigned_to.sys_id.toString());  // push the assigned _to users in recipients array
        recipientsManager.push(reportgr.assignment_group.manager.sys_id.toString()); // push the assigned _to managers in recipientsManager array
    }
    //  gs.log("recipients array: " + recipients.toString());
    gs.log("recipients manager array: " + recipientsManager.toString());
 
    var arrayUtil = new ArrayUtil();
    arrayUtil.concat(recipients, recipientsManager);
    var finalRecipients = arrayUtil.unique(recipients); // unique elements
 
    //  gs.log("finalRecipients: " + finalRecipients);
 
    scheduleReport.user_list = finalRecipients.join(',');
 
    //  gs.log("User list: " + scheduleReport.user_list);
 
    scheduleReport.update();
 
    SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report
 
}

But Can we send individual reports to the managers with only their incident numbers instead of all?

 

Is that possible?

Thank you.

sharan16
Tera Contributor

Hi @Sagar Pagar ,

 

I followed above steps, however wanted to check if it is possible to send scheduled report. For example in my case i want to send list of CIs to owned by's manager. Catch is when i schedule the report. A manager should only receive extract of his owned by and should not receive the entire report. Is this possible? Does the script mentioned above work in this case? 

sharan16
Tera Contributor

Thank you for your quick response Sagar. This script is dynamically changing the recipients of the scheduled email,  but it is not filtering the report (excel) for each recipient only for their relevant data.

 

All the recipients are getting the entire excel extract. 

JR42
Giga Guru

Hi @Sagar Pagar , will you please assist me in getting a report to dynamically send to the Assigned To, and the Manager of the Assignment Group of the tasks in the report?

 

Your script (below) works perfectly for updating the scheduled report users list with the Assigned To and the Assigned To's Manager. For our purposes, we want to send to the Assignment Group manager instead of the Assigned To's manager.  For some reason, nothing I have tried has worked to get this to use the Assignment Group Manager (assignment group.manager).

updateUsersListWithManager();

function updateUsersListWithManager(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheuled report
	scheduleReport.get("70204d1b93734a100271f8fd1dba10eb"); //Sys ID of scheduled Report (not the report)

	var recipients = [];
	var recipientsManager = [];
	var tablename = scheduleReport.report.table;
	var query = scheduleReport.report.filter;

	var gr = new GlideRecord(tablename);
	gr.addEncodedQuery(query);
	gr.query();
	while (gr.next()) {
		recipients.push(gr.assigned_to.sys_id.toString());  // push the assigned _to users in recipients array
		recipientsManager.push(gr.assigned_to.manager.sys_id.toString());  // push the assigned _to managers in recipientsManager array
	}
	// gs.log(recipients.toString());
	// gs.log(recipientsManager.toString());
	var arrayUtil = new ArrayUtil();
	arrayUtil.concat(recipients, recipientsManager);
	finalRecipients = arrayUtil.unique(recipients);   // unique elements

	scheduleReport.user_list = finalRecipients.join(',');
	//	gs.log("User list: " +current.user_list);
	scheduleReport.update();

	SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

}

 

Thanks!

Any help would be greatly appreciated.

VenkatU
Tera Contributor

HI @Sagar Pagar 

 

Thank you for the help. I followed the steps but its not working.  Request help.

 

var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheduled report
scheduleReport.get("I entered my report sys id"); //Sys ID of your schedule Report
 
var tablename = scheduleReport.report.table;
var query = scheduleReport.report.filter;
var users = '';
 
//Execute the report as a GlideRecord with the set table and condition
 
var gr = new GlideRecord(tablename);
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
users += ',' + gr.managed_by;
 
current.user_list = users;
 
}
 
 
current.update();
Luiz Lucena
Mega Sage

Hi friends, 

 

Did anyone found a way to send the scheduled report individually to each manager and the report containing only their subordinates?

I followed all steps @Sagar Pagar provided and is working, but in our case, the report is on the User table, we need managers to provide an update to their contractors and the scheduled report is being sent to ALL managers, in our case, more than 100 managers. 

The report is being sent attached in an Excel format which is not productive for the managers as we need them to quickly look the report and reply to us with an update.

Any idea how to manage this?

raushan
Tera Contributor

Hi Guyz,

I tried the same script to send the email to assigned_to, even tried to create list type report but it seems not working.

Can someone check the code below and suggest. requirement is simple send the scheduled email to incident assigned_to person.

 

updateDynamicRecipients();

function updateDynamicRecipients() {
    var scheduleReport = new GlideRecord('sys_report'); // glide the Scheduled report
    scheduleReport.get("7d63dad82bc7d610f0e8f662ce91bf05"); //Sys ID of your schedule Report

    var recipients = [];
    var tablename = scheduleReport.report.table;
    var query = scheduleReport.report.filter;

    var gr = new GlideRecord(tablename);
    gr.addEncodedQuery(query);
    gr.query();
    while (gr.next()) {
        recipients.push(gr.assigned_to.sys_id.toString()); // push the assigned _to users in recipients array
    }
    // gs.log(recipients.toString());
    // gs.log(recipientsManager.toString());

    var arrayUtil = new ArrayUtil();
    finalRecipients = arrayUtil.unique(recipients); // unique elements

    scheduleReport.user_list = finalRecipients.join(',');
    // gs.log("User list: " +current.user_list);
    scheduleReport.update();

    SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

}
Dhanakoddi2
Tera Contributor

Hello @Sagar Pagar,

 

Thanks for the post. It worked for me. I can see user's list adding in the "Users" field of the scheduled report. But when the email triggers the added users are adding in the 'To' list. Can we add these users in the BCC list?

 

Any others ways are there to add these dynamic users in the BCC of the scheduled report?

 

Regards,

Dhanakoddi

Sagar Pagar
Tera Patron

Hi @Dhanakoddi2 - Yes. We can add users in BCC lists via email notification scripts in notifications.

 

You can try by

i. triggering event via eventQueue() method in scheduled job scripts.

ii. Get  the event parameters in email scripts and call email scripts on scheduled report notification.

gs.eventQueue('event.name', gliderecordObject, usersLists); // Trigger event

 

email.addAddress('bcc', usersLists); // to add BCC users in notification

 

${mail_script:email.script.name}

 

Thanks,

Sagar Pagar

surajbiswal
Tera Contributor


Expected Result: I am trying to set up an email notification system that triggers an email to the task assignee and their manager if a change task is pending for more than 3 days. The email should contain all pending change task details in an Excel sheet. If there are multiple change tasks with different assignees, separate emails should be sent to each assignee with their respective pending task details.

Currently, one email is being sent to all assignees with all pending change task details." how to achieve?

Version history
Last update:
‎12-01-2022 04:30 AM
Updated by:
Contributors