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
Harish27
Kilo Guru

Hi,

I want to set the columns for the report from the script. How can we do that?

TIA.

Regards,

Harish M

Cris AD
Tera Contributor

I followed all the steps and nothing works.. Im in Quebec version.

is not updating the user_list field... any idea?

Sagar Pagar
Tera Patron

Hello Cristobal,

Please create a scheduled script and add the on demand scheduled report sys_id in it(updated step:3).

 

Regards,

Sagar Pagar

Cris AD
Tera Contributor

Hello Sagar, thanks for responding,

So I need to update the field Users ( the recipients) in the form of one scheduled report to be dynamic values.

 I have created the scheduled report with one user because this one will be always as default, but I need to add more users  to the list.

I have selected the "conditional" option to create one script that makes a query to a table, I get an array variable with all the sysid for the users as unique. 

So when I run the script on background to verify the values, I got the correct sysids :

example:

 *** Script: assignedUnique = ed83fc7a1b0633806278edf9bc4bcb12,978178521baa77846278edf9bc4bcbdf,ce47701e1bae77846278edf9bc4bcbee, etc

 

so when I try to update the current form for the scheuled report to add the new recipients nothing changes and only send the report to the original default user that I put manually.

 

 

but when I execute it, nothing happens and is only sending the notification to first user that I enter manually in the field, is not adding/updating the other recipients, its like the script was ignored.

 

Any idea how can I make this work?

Cris AD
Tera Contributor

Hello Sagar, thanks for responding,

So I need to update the field Users ( the recipients) in the form of one scheduled report to be dynamic values.

 I have created the scheduled report with one user because this one will be always as default, but I need to add more users  to the list.

I have selected the "conditional" option to create one script that makes a query to a table, I get an array variable with all the sysid for the users as unique. 

So when I run the script on background to verify the values, I got the correct sysids :

example:

 *** Script: assignedUnique = ed83fc7a1b0633806278edf9bc4bcb12,978178521baa77846278edf9bc4bcbdf,ce47701e1bae77846278edf9bc4bcbee, etc

 

so when I try to update the current form for the scheuled report to add the new recipients nothing changes and only send the report to the original default user that I put manually.

 

 

but when I execute it, nothing happens and is only sending the notification to first user that I enter manually in the field, is not adding/updating the other recipients, its like the script was ignored.

 

Any idea how can I make this work?

Sagar Pagar
Tera Patron

Hello Cris,

You are on the Quebec version, and the current object in the conditional script is not working. hence it is not going to update the user's list. (Mentioned in above Article step:5)

You need to update the user's list by scheduled script.

 

1. Change this scheduled report to "On-demand" and save it.

2. Create a new scheduled job with "Automatically run a script of your choosing" with the same scheduling time. Refer: Automatically run a script of your choosing

find_real_file.png

Add this script and add you're on-demand scheduled report sys_id.

updateDynamicRecipients();

function updateDynamicRecipients(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheduled report
	scheduleReport.get("sys_id of report"); //Sys ID of your ondemand 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());

	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

}

 

Regards,

Sagar Pagar

rogerkali
Kilo Contributor

Thanks for sharing.

 

 

snaptube vidmate

Cris AD
Tera Contributor

Hello Sagar,

 

This works!!, you are amazing!, thank you so much for helping me in this!

 

Regards

 

Dheeraj Kumar1
Kilo Explorer

Hi Sagar,

I have tried and followed every steps as mentioned but no luck. I have tried to triggered email to users manger who was not active since last 50 days.

Could you please help me. I have attached the screenshot of code.

 

Regards,

Dheeraj

Sagar Pagar
Tera Patron

Hello Dheeraj,

Would you please share your on-demand scheduled report for the same.

 

Regards,

Sagar pagar

Dheeraj Kumar1
Kilo Explorer

Hi Sagar,

Thanks for the reply. Please find below code for on demand report along with the attached screenshot

 

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.user_name.manager.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

}

 

 

Sagar Pagar
Tera Patron

Hello Dheeraj,

You have shared the scheduled script.

You have to create an on-demand scheduled report(step:1 and step: 2) first and then used that report sys_id to trigger it via scheduled script and update the user's manager as recipients. 

 

Regards,

Sagar Pagar

Dheeraj Kumar1
Kilo Explorer

Hi Sagar,

 

I have already created on demand scheduled report. Kindly see the attached screenshot.

 

Regards,

Dheeraj

Mayura1997
Tera Expert

Hi,

I have tried it but iam getting 2 mails.  There is 'Time' field on both scheduled jobs so it might the thing iam getting 2 mails at same time. Could you please help me with this.

send a scheduled report to the users based on the table column of the report - IT Service Management...

schedule report:

find_real_file.png

 

scheduled script execution:

find_real_file.png

Thanks.

michi3
Mega Explorer

same here. 

michi3
Mega Explorer

do you manage to get it run now?

Ankur Bawiskar
Tera Patron
Tera Patron
karan15
Tera Contributor

were you able to send the emails only to selective users who were listed as for e.g as owner of application or certificate 

Daniel Voutt
Tera Contributor

Hi,

This is working great, however, I would like to take it a step forward. 

I have a dynamic report (assigned_to is (dynamic) Me) that would need to be sent to the recipients individually as the content would be specific to them recipient, and so I think I would need to loop it so that it sends v1 of the report to user a, then v2 of the report to user b, and so on.  I may have anything up to 100 recipients.

Any guidance on how I might be able to achieve this?

Thanks in advance,

Dan

JR42
Giga Guru

@Sagar Pagar Will you please help me figure out why I am not able to get this to work?  I've created the report, schedule and script, but no emails are generated when I click Execute Now or when the scheduled time for the script comes.  The report schedule User field is not being updated either.

The report is called, Expired Due Date, and is returning one record.  I have added the assigned_to and assigned_to.manager columns. (note that the assigned_to.manager column header displays as 'Manager'.

This is the schedule for the report:
find_real_file.png

 

This is the Scheduled Script (it has the sys_id of the report):

updateUsersListWithManager();

function updateUsersListWithManager(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheuled report
	scheduleReport.get("1d2412021b2cac90499233fccd4bcba5"); //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
		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(senderArray);   // unique elements

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

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

}

 

Any help with getting this working would be greatly appreciated.

Thanks!

Sagar Pagar
Tera Patron

Hi Justin,

pls, share your scheduled report complete screenshot and the report should contain the "Assigned to and AssignedTo.manager" column.

Regards,

Sagar Pagar

JR42
Giga Guru

Hi Sagar, thank you for responding.

 

Here is the screenshot of the report.  The column labeled 'Assigned To' is the the assigned to user, and the column labeled 'Manager' is the assigned_to.manager.  The sys_id is 1d2412021b2cac90499233fccd4bcba5

find_real_file.png

 

Here is a screenshot of the column configuration showing the Assigned to and Assigned to.Manager:

find_real_file.png

 

Here is the full screenshot of the schedule for the report:

find_real_file.png

 

Do you notice anything I am doing wrong here that would prevent it from working?

Thanks again for your support!

Sagar Pagar
Tera Patron

Hi Justin,

1) You have to define recipientsManager array in your scheduled script at line no 7, it should be


	var recipients = [];
	var recipientsManager = [];

2) Update your line 24 to get the unique users sys_id in finalRecipients.

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

also, comment out the logs to print the script logs for troubleshoot.

 

Regards,

Sagar Pagar

JR42
Giga Guru

Thanks, Sagar.  It is working! 

 

Shakeel Shaik
Giga Sage
Giga Sage

Great Article.

Thanks for sharing.

karan15
Tera Contributor

hi 

 

is there any script we can add to send out report if incident is in pending state and without update for last 5 days ?

Sagar Pagar
Tera Patron

Hi Karan,

You can apply the required filter in incident list view, copy the query and use the copied query in reports.

 

Thanks,

Sagar Pagar

Sathiskumar_D
Giga Sage

Thanks. Is this feature available in ServiceNow? I read in community that "to" is not feasible. Only "cc" and "bcc" are available.

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

Sagar Pagar
Tera Patron

Hi Satish,

Good catch! Thanks for commenting. Let me correct it.

karan15
Tera Contributor

Hi 

I tried using script to include manager but having this error as attached , please help as am not too good with scripting 

 

 

Sagar Pagar
Tera Patron

Hi, missing  } at the end of function at line no 34. just include it and save it.

Pavansairam SV
Tera Explorer

Hi Sagar,

 

I had created report and scheduled email job is set but my criteria here is i need to send it to only assignment group managers can you please share the scheduled script for that scheduled email job.

Sagar Pagar
Tera Patron

Hi @Pavansairam SV,

 

Follow the stpss mentioned above and try the below scripts -

Here is the scripts to send scheduled reports to assignee's managers only. modify it accordingly.

 

updateUsersListWithManager();

function updateUsersListWithManager() {
	var scheduleReport = new GlideRecord('sysauto_report'); // glide the Scheuled report
	scheduleReport.get("1d2412021b2cac90499233fccd4bcba5"); //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()) {
		recipientsManager.push(gr.assigned_to.manager.sys_id.toString()); // push the assigned _to managers in recipientsManager array
	}

	var arrayUtil = new ArrayUtil();
	finalRecipients = arrayUtil.unique(recipientsManager); // unique elements
	scheduleReport.user_list = finalRecipients.join(',');

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

}

 

Thanks,

Sagar Pagar

Haresh Haru
Tera Expert

Hi sagar pagar,

 

I have followed the above steps but it's not working, can you please help me

Regards

Haresh

Sagar Pagar
Tera Patron

Hi @Haresh Haru - Can you share your requirement and scripts here?

 

Thanks,

Sagar Pagar

GB14
Kilo Patron

Hello @Sagar Pagar 

 

I followed your steps and used this script (Step 4: If need to include assigned _to Manager or assignment groups manager, then use the script.)

Executed the script and Then it added the users and their managers to the list. 
Nothing showing under the logs in my personal dev instance. 

Am I missing anything?

Do I have to modify the script?

Please assist. 
Thanks,
G

 

Sagar Pagar
Tera Patron

Hi @GB14,

Can you share complete scripts here?

Thanks,

Sagar Pagar

GB14
Kilo Patron

@Sagar Pagar  Its working now. something on the report end. 
Thank you!

Karin Duijnker1
Tera Contributor

Hi Sagar, thank your for this article. I want to send the automatic email to the manager of the assignment group. I have created a report, a scheduled email and a script. But I cannot get it work.
The report:

KarinDuijnker1_3-1675695045841.png

 

 

The last column is the name of the manager.... Field from Task table.

The scheduled report:

KarinDuijnker1_2-1675695010820.png

The script:

updateUsersListWithManager();

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

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

var gr = new GlideRecord('task');
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
recipientsManager.push(gr.assignment_group.manager.sys_id.toString()); // push the assignment group manager in recipientsManager array
}

var arrayUtil = new ArrayUtil();
finalRecipients = arrayUtil.unique(recipientsManager); // unique elements
scheduleReport.user_list = finalRecipients.join(',');

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

}

 

What am I doing wrong?

jyothi15
Tera Expert

@Karin Duijnker1  

I had same issue as you mentioned .I  added var recipientsManager[ ];
This should work.

ESanchez013
Tera Expert

The Scheduled Script Execution doesn't seem to work. It doesn't auto-run nor does it do anything when I click [Execute Now].

Sagar Pagar
Tera Patron

Hi @ESanchez013,

Can you share what you want to achieve and what you tried?

 

Thanks,
Sagar Pagar

ESanchez013
Tera Expert

Hi, @Sagar Pagar sure, I have a table for hours worked and I want to email the hours worked to the worker (user) and their manager individually, so I changed the name of the reference field but otherwise kept your script intact.

 

See below code:

updateUsersListWithManager();

function updateUsersListWithManager(){
	var scheduleReport = new GlideRecord('sysauto_report');  // glide the Scheuled report
	scheduleReport.get("3bd0198d87da69100edbb80e8bbb3516"); //**I changed this too** 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.user.sys_id.toString());  // **I CHANGED THIS**
		recipientsManager.push(reportgr.user.manager.sys_id.toString());  // **I CHANGED THIS ALSO** 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

}

 

It doesn't run.

AdamUMC
Giga Guru

@ESanchez013 

 

I'm totally no expert, but I'm giving it a try;
Your script looks fine, the only problem might be your reference to where the script can find her required input to mail your report. I think this is part is your main problem and you should first fix before you can execute your scheduled script successfully;

 

If your report has no Column for "users" at the table from which you are reporting, it won't work I think.
All this people from above are reporting at the Incident table for example, and the Incident table and Incident form contain fields like "assignment_group" and "assigned_to".

recipients.push(reportgr.user.sys_id.toString());
recipientsManager.push(reportgr.user.manager.sys_id.toString());

AdamUMC
Giga Guru

@Daniel Voutt 
@Sagar Pagar 

I need exactly the same as Daniel Voutt, see this post of Daniel. is there no solution to achive this?
I can send reports to the Managers of Assignment Groups, but one (1) report is send to all Managers within one (1) mail that contains an counting overview of all groups.

I want per Manager a mail that reports only his groups as Group Manager.


Dasari Srihari
Tera Contributor

Hi @Sagar Pagar , How to send notification to Assigned to and Opened by. Both recipients i want send notification. 

Sagar Pagar
Tera Patron

Hi @Dasari Srihari, you have to push assigned to and Opened by users to include as recipients.

 

Sample logic:

recipientsAssigned_to.push(grObject.assigned_to.manager.toString()); // push the assigned _to managers in recipientsAssigned_to array
recipientsOpenedBy.push(grObject.opened_by.toString()); // push the opened_byin recipientsOpenedBy array

 

Thanks,
Sagar Pagar

Alex199
Tera Explorer

Hi @Sagar Pagar ,

 

Thanks for this! it is pretty awesome! Just wondering if you could confirm something for me. This works absolutely perfect for reports that run on the incident table; but when I use it for incident_sla table, I get no values for the Manager column. Is there something I would need to do?

 

I can confirm that the report (on the incident_sla table) itself contains the assignment_group.manager column:

Alex199_0-1698951949437.png

 

but when I output to the syslog, I can see no values for the recipients manager array (but there are quite a few commas, which seems like it is looking at something?):

Alex199_1-1698952014636.png

 

Using the same exact scheduled script, but just updated the report to one that is for the incident table, you can see the recipients manager array populates:

Alex199_2-1698952177994.png

 

Any ideas?

 

Thank you!

kpchrisman
Tera Explorer

Schedule a breach SLA report and going to send to recipient which is assigned to and assigned_to.manager but not work. 

Sagar Pagar
Tera Patron

Hi @kpchrisman,

Can you share your steps and scripts here?

 

Thanks,

Sagar Pagar

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