The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Changing filter for scheduled report

Ryan S
Kilo Sage

I need to send a scheduled report to users but have the report filter changed so it's only records pertinent to the user. For example, a report of all incidents where "Assigned to" is the recipient. If I have a group with 5 people in it, then each of those 5 people would receive the report with only the incidents assigned to themselves.

Thanks in advance.

1 ACCEPTED SOLUTION

EDIT: I've finally got it working. Ankur I found some of your other posts and putting things together it worked. This method uses a prebuilt report that is scheduled. I then have to update the report filter and the scheduled report distribution. A wait timer was required as well otherwise only the last person was getting a report.

 

var ReportDistribution = Class.create();
ReportDistribution.prototype = {
  initialize: function() {},

  sendFilteredReportToGroup: function(reportSysId, groupSysId, scheduleID) {
    var grMembers = new GlideRecord('sys_user_grmember');
    grMembers.addQuery('group', groupSysId);
    grMembers.query();

    while (grMembers.next()) {
      var userSysId = grMembers.user.sys_id.toString();
      var userEmail = grMembers.user.email.toString();
      gs.info(userEmail);
      this.sendFilteredReportToUser(reportSysId, userSysId, scheduleID, userEmail);
    }
  },

  sendFilteredReportToUser: function(reportSysId, userSysId, scheduleID, userEmail) {
    try {
      var grReport = new GlideRecord('sys_report');
      grReport.addQuery('sys_id',reportSysId);
      grReport.query();
      if (grReport.next()) {
        var grUser = new GlideRecord('sys_user');
        grUser.query(userSysId);
        if (grUser.get(userSysId)){
            var myFilter = 'active=true^caller_id=' + userSysId;
            gs.info(myFilter);
            grReport.filter = myFilter;
            grReport.update();

            var scheduleReport = new GlideRecord('sysauto_report');
            scheduleReport.addQuery('sys_id',scheduleID);
            scheduleReport.query();
            if (scheduleReport.next()) {
                scheduleReport.address_list = '';
                scheduleReport.update();
                scheduleReport.address_list = userEmail;
                scheduleReport.update();
                SncTriggerSynchronizer.executeNow(scheduleReport);

                //Only last person was receiving email, so adding a wait timer and it fixed it
                var seconds = parseInt(10, 10) * 1000;
                var start = parseInt(new Date().getTime()) + seconds;
                while(start>parseInt(new Date().getTime())){
                // do nothing
                }

            }
       } else {
            gs.error("User with sys_id " + userSysId + " not found.");
        }
      } else {
        gs.error("Report with sys_id " + reportSysId + " not found.");
      }
    } catch (ex) {
      gs.error("Error sending report: " + ex.message);
    }
  },

  type: 'ReportDistribution'
};
 

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Ryan S 

does your report have filter condition as Assigned to Dynamic me?

If yes then how are you generating the scheduled report?

Are you including email body in it? if yes then why not provide link to the report in that email body

When the users receive this email and they click on link it shows reports for incidents where assigned to is him/her and then they can export it?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I need the report to be provided via email for access external to ServiceNow. I tried having the 'Assigned to dynamic Me' but that generates the file emailed out is then the same to every person (it's only filtered based on user record that system uses to generate the scheduled report).

 

I started to look into scripting the report, but running into issues with it either setting the filter properly or actually generating the email, so wanted to see what input the community had before going much further down that path.

Hi @Ryan S 

I do not think you can achieve this with one scheduled report for all recipients using standard reporting functionalities.

@Ryan S 

you can do this but it's very custom solution and you will have to test it thoroughly

Note: remember this solution might impact if user is seeing report on front end during the time when the scheduled report runs

1) have a scheduled job

2) get the group and iterate for each group member

2) query sys_report and update the filter condition dynamically i.e. Assigned to = Member 1

3) then run the scheduled report via script so that it sends report only for that Member/User

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader