Help please! How to dynamically set recipients of an existing Scheduled Report to users that are in the report's output.

sbutton
Kilo Expert

Hi,

I'm working on trying to configure a Conditional Scheduled email of a report.

I've been following resources - Article

I'm not quite sure why this isn't working as logically it makes sense to me that it should work.

I'm querying the Asset table (alm_asset) and trying to add the Managed by users to the user.list on the schedule report. 

the only method to verify success is by checking the email logs and nothing is appearing. in my test report there's 5 records and 3 confirmed Managed by users in the list.

i've been looking at it a while and my eyes are starting to hurt. Can anyone highlight something i am doing wrong?

Appreciate any advice

function lookupUsersAndUpdateReport(){


   // look up "managed_by" people -- assumes "table" is alm_asset


   var senderArray = [];



   var taskQueryGR = new GlideRecord(current.report.table);


   taskQueryGR.addEncodedQuery(current.report.filter);


   taskQueryGR.query();


   while (taskQueryGR.next()){


           senderArray.push(taskQueryGR.managed_by+ '');


   }



   current.user_list = senderArray.join(','); 


   current.setWorkflow(false);


   current.update();


   current.setWorkflow(true);



   // only return true if there were records listed in the result set


   if (senderArray.length > 0){


           return true;


   }


   return false;


}

lookupUsersAndUpdateReport();



//my report query - managed_byISNOTEMPTY^substatus=Vendor Borrowed^assigned_to.active=false

 

 

1 ACCEPTED SOLUTION

sbutton
Kilo Expert

I have found the solution to this now. 

Oversight on my part.

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

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

Thanks,

Sean

View solution in original post

14 REPLIES 14

Hello,

Thank you very much for this solution!
Worked like a charm!

With regards
Anton

Aniruddha Das
Tera Contributor

How to call the "Scheduled Job" which is created for the report?

// REMINDER: below will apply ONLY if 'Conditional' checkbox is checked above
var RunAnswer = false; // only flip this to true if email needs to go out
var scheduleReport = new GlideRecord('sysauto_report'); // get details directly from the Scheduled Report
scheduleReport.get(current.sys_id); // gets the Sys ID of THIS particular Scheduled Report
var tablename = scheduleReport.report.table; // gets the table that the above Report points at
var query = scheduleReport.report.filter; // gets all the Report's predefined clauses and other critieria
var UserList = current.user_list; // gets the predefined User recipients of this report
var gr = new GlideRecord(tablename); // set up a local GlideRecord object using the Report's table
gr.addEncodedQuery(query); // apply to it all the Report definitions found above
gr.query(); // execute the Report query into the GlideRecord object
// ----------------------------
while (gr.next()) { // loop through each record until done

...

 

 

NOTE: In a Scheduled Job conditional Javascript, "current" refers directly to the Scheduled Job being executed. Beware of temptation to include a line like "current.update()", as that will make any "current" attribute changes you made (during run-time) PERMANENT to that Scheduled Job.  That might be handy for some jobs but could be unknowingly bad for others.

Someone please explain what this line is pushing 

while (gr.next()) {
    recipients.push(gr.managed_by.sys_id.toString());
}

Author (sbutton) originally thought to keep new email recipients in a separate array. Later however I learned I could just use the 'current' object's .UserList attribute instead (eg, current.user_list += gr.managed_by;).  Just make sure you do NOT include a line such as 'current.update();'.  That will overwrite your Scheduled Job permanently with any updates you made to it within the script.  Making them permanent is not necessary.

The original post is from years ago.