HELP! How to dynamically set recipients of an existing Scheduled Report to email addresses that are in the report's output?

scottbjorke
Tera Contributor

I have a lot of Scheduled Reports that run a given Report whose output includes email addresses. Presently, my Scheduled Reports get emailed only to whoever I have pre-defined as the STATIC list of email addresses, per the Scheduled Reports UI (in this case, 'bob@mycomany.com'). From there, those recipients have to open the attached report, spot the email address on each row, and FW the report attachment directly to those email addresses from the report output. This seems unnecessary -- I'd rather "cut out the middle man" and have SN set the recipients equal to the email addresses I find within the report.

I assume this means:

- I can simply 'utilize' the same Report that is already defined in my Scheduled Report

- set Conditional = TRUE then insert my scripting logic into the Scheduled Report definition

- walk through some kind of GlideRecord that essentially IS my predefined Report

- get each record's email address (which is one of its report columns) -- but be aware I don't know exactly how to get its exact name because I'm dot-walking and don't have admin rights to utlize the SN log

- appending it to something like "current.address_list" (with a trailing comma to separate each email address)

- that current.address_list can hold a long comma-delimited list of emails as a string value and is not an array[] instead

- retuning TRUE to indeed trigger the report.

Of course, any or all the above assumptions may be really lousy     And of course, there's probably a much easier, simpler way to do what I want to do.

Please note I do not have admin privileges. I'm just a Change Manager trying to reduce operational waste and learn more about leveraging SN's capabilities myself.

Here's header and the script of my latest attempt.   Would really appreciate it if someone could take a look and reply with ideas/comments that are indeed helpful.   That also includes giving me code examples that essentially replace mine (ie, starts over).

Please note I have confirmed the below approach does successfully send my report output to the STATIC recipient from the script AND to the email address predefined in the UI below.   I just can't figure out how to glean additional email recipients from the addresses found in the report output.

find_real_file.png

---

// REMINDER: THE BELOW SCRIPT EXECUTES ONLY IF "CONDITIONAL" IS CHECKED ABOVE

// Include the Script Include
gs.include('ReportEmailList');

// Set the STATIC recipients first
var email_recipients = 'john.doe@mycompany.com';
var next_email = ' ';

//Execute the scheduled report within a local GlideRecord
var rec = new GlideRecord('sysauto_report');
rec.get('name', 'Scott some rows to return various Release Owners'); // this is same as what's in the header
rec.query();

// cycle through each report row (if any), get the DYNAMIC recipient value, and append it to the recipients string with a leading comma
if (rec.getRowCount() > 0) {
      while (rec.next()) {
                next_email = rec.GetValue('u_owner.email');    
                email_recipients += ',' + next_email;
      }
}

current.address_list = email_recipients;       // FYI - I have verified this line works to affect email recipients manually -- just need help setting 'email_recipients' variable using report output
// Need to set this true for condition script to run
answer = true;

---

PS   Here's how my Report is defined:

find_real_file.png

1 ACCEPTED SOLUTION

I modified your solution a bit and came up with the following to fit the requirement.


I'm not sure why you set a static recipient in the code, this could be written in the Email addresses field directly.


Instead of adding the users to the Email addresses field I added them to the Users field instead (see below script)



var scheduleReport = new GlideRecord('sysauto_report');


scheduleReport.get(current.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.opened_by;


}



current.user_list = users;


current.update();



// Need to set this true for condition script to run


answer = true;



This script works fine for me when testing in my developer instance.


View solution in original post

21 REPLIES 21

sbutton
Kilo Expert

Hi, How did you verify success of this. i'm trying to replicate and am not seeing any emails in the email log "created today" 

 

I tested this exact script below using task table but nothing appears to happen. are there any other prerequisites? 

created by sys admin

var scheduleReport = new GlideRecord('sysauto_report');


scheduleReport.get(current.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.opened_by;


}



current.user_list = users;


current.update();



// Need to set this true for condition script to run


answer = true;

Chandra51
Tera Contributor

I am using below code & still I am not getting any email. Could you please help? (Owned by is one of the field in my report)

 

var scheduleReport = new GlideRecord('sysauto_report');
scheduleReport.get(current.sys_id);
var tablename = scheduleReport.report.table;
var query = scheduleReport.report.filter;
var users = '';
var gr = new GlideRecord(tablename);
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
users += ',' + gr.owned_by;
}
current.user_list = users;
current.update();
answer = true;

Hey Chandra,

current.update does not work on a server side script which is what a scheduled email of a report is when using conditional option.

1. Create the report

2. Create the schedule of the report on demand.

3. Add the required subject and intro message. and file output (pdf, xlsx, etc)

4. Create a separate Scheduled job (script execution) and apply the script in the script field

var scheduleReport = new GlideRecord('sysauto_report');
scheduleReport.get('d742dd441bc4d410b43d0f69cc4bcb69'); //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.managed_by.sys_id.toString());
}
var arrayUtil = new ArrayUtil();
var finalRecipients = arrayUtil.unique(recipients);
scheduleReport.user_list = finalRecipients.join(','); //get unique recipients
scheduleReport.update();
SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

You can change gr.managed_by to gr.owned_by

This overrides the previous set of user on the user_list and executes the report.

Let me know if this helps.

Thanks,

Sean

I used below script as you mentioned but email is not coming, I hard coded names in user_list glide list & I got the email.

Could you please help

var scheduleReport = new GlideRecord('sysauto_report');
scheduleReport.get('f7fde516db1a1890182f5bd2ca9619b2'); //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.owned_by.sys_id.toString());
}
var arrayUtil = new ArrayUtil();
var finalRecipients = arrayUtil.unique(recipients);
scheduleReport.user_list = finalRecipients.join(','); //get unique recipients
scheduleReport.update();
SncTriggerSynchronizer.executeNow(scheduleReport); //execute schedule report

Does your report have a number of different owned_by records in the list?

(scheduleReport.report.filter)