Scheduled Report creates TWO attachements

spike
Mega Sage

I want to be able to run a report when a user raises a service request. It will be an extract of some data and emailed to them.

To achieve this my plan is to create a Flow with Flow Designer and have that execute when the request is raised.

To facilitate the executing of the script I've created a new Action with the following code:

 

(function execute(inputs, outputs) {

  var scheduled_report = new GlideRecord('sysauto_report');

  //Set the scheduled job to active
  scheduled_report.active = 'true';

  //We add report title and user name to the name for clarity
  var random = Math.floor(Math.random()*90000) + 10000;
  scheduled_report.name = 'AutoReport - ' + random;

  //Run report as given user to show only his records
  scheduled_report.run_as = 'xxxxxx891bc105105915848cd34bcbbd'; //gs.getUser();

  //Sent report to same user, so he receives his own report
  scheduled_report.user_list = 'xxxxxx891bc105105915848cd34bcbbd'; //gs.getUser();

  //Only send the report once

  //It will be rescheduled if needed
  scheduled_report.run_type = 'once';
  scheduled_report.report_title = 'AutoReport';
  scheduled_report.report = inputs.report_sys_id;
  scheduled_report.omit_if_no_records = 'false';
  scheduled_report.output_type = 'XLSX';
  scheduled_report.page_size = 'A4';

  //Now, insert the newly created scheduled job to start running
  //As it is a "once" job, it will run automatically
  var answer = scheduled_report.insert();

})(inputs, outputs);

 

 

The code actually works. A scheduled report is created and executed. However despite setting the output type to XLSX I'm getting both an XLSX and a PDF attached to the email that is sent.

Can anyone tell me why? And how to fix it? I've been able to find anyone with a similar problem!

(I had been looking at some previous work about On Demand reports that @Chris Fulton had talked about but that doesn't appear to be available anymore).

Many thanks!

1 ACCEPTED SOLUTION

spike
Mega Sage

I've solved the issue. While I'd looked at the content of the PDF, I hadn't realised it's significance. It said:

"Access to this content denied based on report_view ACLs."

 

A bit of browsing and a light blub moment later, I added an ACL that allowed the user running the report to read the report_view of the table in question.

 

PDF disappeared and I got the one attachment I was expecting.

View solution in original post

4 REPLIES 4

Chris Fulton
ServiceNow Employee
ServiceNow Employee

Spike,

 

Here is the code I used when I wrote the "on-demand" reports.   I don't believe it's going to be much help for what you are seeing though.   I haven't done much scripting with reports in a long while and don't have an answer as to why it's giving you both PDF and XLS.

 

//*********************************************
// Retrieves a scheduled report, updates with 
// data from the record producer, then 
// executes the report. 
//
// SetAbortAction will keep this from creating
// a record in the table associated with this
// record producer.
//*********************************************
 
 
 
// jobname will be the name of the scheduled report.
// it will be re-used each time this request is made.
// Simply create one, set to inactive and use the name 
// here.
var rndnum = Math.floor((Math.random() * 1000000) + 1);
var jobname = "Rpt_On-Demand_" + rndnum;
var rec = new GlideRecord('sysauto_report');
 
 
// These lines will update the report job
// with fields from the record producer.
rec.name = jobname;
rec.active = false;
rec.address_list = producer.email;
rec.report = producer.report;
 
// Save the updates
rec.update();
 
 
// Trigger the "Execute Now" button on the form.
if (typeof SncTriggerSynchronizer != 'undefined')
   SncTriggerSynchronizer.executeNow(rec);
else
   Packages.com.snc.automation.TriggerSynchronizer.executeNow(rec);
 
 
sleep(1500);
 
var gr= new GlideRecord('sysauto_report');
gr.addQuery('name',jobname);
gr.query();
gr.next();
gr.deleteRecord();
 
 
//Abort the next action (update in this case)
current.setAbortAction(true);
 
 
function sleep(miliseconds) {
   var currentTime = new Date().getTime();
 
   while (currentTime + miliseconds >= new Date().getTime()) {
   }
}

Thanks for this. I was wondering how to trigger the report but leave it inactive. This has helped there.

 

You're right though, still doesn't help me with the two attachments. And it still happens using your trigger code, rather than mine.

 

Interestingly, even once the row is inserted into sysauto_reports, if I manually execute it, it still gets two attachments. Got to be something in how the record is held but I can't see anything on the table or in the log to indicate why.

spike
Mega Sage

I've solved the issue. While I'd looked at the content of the PDF, I hadn't realised it's significance. It said:

"Access to this content denied based on report_view ACLs."

 

A bit of browsing and a light blub moment later, I added an ACL that allowed the user running the report to read the report_view of the table in question.

 

PDF disappeared and I got the one attachment I was expecting.

Chris Fulton
ServiceNow Employee
ServiceNow Employee

Spike,

 

I'm glad to hear you were able to resolve the issue.  Sometimes it's the little things we overlook - I do it as well.  I wouldn't have thought to look there for an error message either but I'm glad you did.