Generate a report from a workflow run script

Brian24
Tera Expert

All,

On submission of a catalog item I have a workflow that I intend to generate a report from alm_asset and email it to the requester. I've restricted it managers in our organization and setup the report so it contains all assets where assigned_to.manager is dynamic. This part is working fine.

The issue I'm having is with the automatic report generation and setting the requester's email address in the sysauto_report record.

In the Workflow Run script I have the following:

Neither commented line adds the current users email address to the address_list field on the sysauto_report form.

var gr = new GlideRecord('sysauto_report'); 
gr.get('name', 'ReportNamehere');
gs.info('User email address' +gs.getUser().getEmail());
//gr.setValue('address_list', gs.getUser().getEmail());
//gr.get('address_list', gs.getUser().getEmail());
SncTriggerSynchronizer.executeNow(gr);

This prevents an email from being generated to the current user.

1 ACCEPTED SOLUTION

use below script. in this script, you can pass sys_id of users in user_list variable.

 

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


        scheduled_report.name = ''


                 + 'Scheduled Email Report of '


                 + report.title


                 + ' - '


                 + user.name;




        //Run report as given user to show only his records


        scheduled_report.run_as = user.sys_id;


        //Sent report to same user, so he receives his own report


        scheduled_report.user_list = user.sys_id;


        //Only send the report once


        //It will be rescheduled if needed


        scheduled_report.run_type = 'once';


        scheduled_report.report_title = subject;


       scheduled_report.report_body = body;


       scheduled_report.report = report.sys_id;


       scheduled_report.omit_if_no_records = omit;



        //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();

View solution in original post

6 REPLIES 6

Hey Sachin,

This worked like a charm. However i want to set the columns of the report while creating it. Is it possible? If yes can u please help on that.

I am generating a report on a staging table. I want to show error records in the report along with few custom fields. So while generating the report from workflow am unable to add/show these in the report. can u pls let me know how we can set columns for the report? 

TIA.

Regards,

Harish M

Brian24
Tera Expert

I was able to resolve the issue by adding an initial glideRecordQuery to update the scheduled report before triggering it.

 

var usr = gs.getUser();

var rs = new GlideRecord('sysauto_report');
rs.get('sys_id', '<sys_id>');
rs.setLimit('1');
rs.user_list = usr.email.toString();
rs.update();

var gr = new GlideRecord('sysauto_report'); 
gr.get('sys_id', '<sys_id>');
SncTriggerSynchronizer.executeNow(gr);