Programmatically change scheduled report filter

stevenm
Kilo Guru

I have a user request as follows.  We have a monthly knowledge base report of upcoming expiring articles.  This could contain articles from quite a few different authors.  I have been asked to find out if I can send out that report to each author but have it contain only their expiring articles.  

Right now the report is defined with filter of Published articles and valid date within the next 30 days.  So I'm looking for a way to programmatically include the author.  In a scheduled job script I have aggregated the authors using the reports filter but now I have to feed that filter + author back to the report and send it out.  I'm not finding a way to do that even though I have found one way to try.  I don't think I can modify the filter on a pre-defined report through a job.  I just need to know if it can be done. If not, I have my answer. 

5 REPLIES 5

Ravi9
ServiceNow Employee
ServiceNow Employee

if i may , can i suggest a diff approach ! it will be a little bit more work , up to you ...

first , tweak the report (or create a copy! whatever best) such that it includes all your existing condition + author is logged in user

next , create a scheduled job which runs say once a week and look for those about to expire articles (same filter as report) , it groups by the author and then queues an event  for each author thus a notification , the email would contain the link to report , author gets  the link , opens it and voilà!

I tweaked the report to now have the additional filter of Author is dynamic.  

I created a scheduled job as you can see in my attachment.  I am having a terrible time testing it though.  I made it on demand and click Execute Now and nothing happens.  I don't know how to get this thing to run.  

Hi Steven,

In your scheduled job, the second parameter to gs.eventQueue() method is a GlideAggregate object (This method expects a GlideRecord object as second parameter), that is why the event generated is erroring out and not processing, as a result it will not send an email.
You can go to "system logs">"events" and check it in a min after job execution.
You may pass "current" without quotes as the second parameter, just to make sure that event records capture "instance" as schedule job definition sys_id.
But in the notification mail scripts do not use "current." anywhere as you may not have much use using any of the fields.

After event processing if you want to check whether emails are generated or not.

Since our lower instances or OOB instances dont have email sending enabled, you may go to system logs and emails and check with the subject line to see if emails are generated to the respective end user mails.

In my other reply, I have given step by step process on how to trigger notifications to users and include either direct instance list view link or export link. 

 

Thanks and regards,

Subrahmanyam Satti

 

Subrahmanyam2
Giga Guru

Hi Stevenm,

As far as I know, your ask may not be possible.

But if you relax the need to send attachments in email and go with sending the link to the list of articles that requires user attention to each unique Author who has articles expiring with in 30 days you can do that easily. (In the below approach, the system also gives the list of articles already expired too).

Note: At the end of the article, I suggested an approach using which you can form a link which directly exports the data from instance in excel format.

Please find the steps I have taken to do this:

1) Go to "System Policy" > "Events" > "Registry" in the system and register a new event.

find_real_file.png

2) Go to "System Notification" >"Email" > "Notification Email Scripts" and create a new script as shown below:

find_real_file.png

Since I have less data in OOB instance I kept valid_to greater than today in the encoded query in screenshot.

In you case you may want to modify it as shown in below script block. (In the filter condition, I am using "Assigned to is (dynamic) Me" query as one of the conditions) so that I need not write additional logic to find each user and modify filter in the scheduled job.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var kbArticlesLink = gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=kb_knowledge_list.do?sysparm_query=workflow_state=published^valid_toRELATIVELT@dayofweek@ahead@30^authorDYNAMIC90d1921e5f510100a9ad2572f2b477fe";
    template.print("Please click <a href='" + kbArticlesLink + "' target='_blank'>here</a> to navigate to the list of articles expiring soon!");
})(current, template, email, email_action, event);

 

Along with this you may want to provide links to each article that is pending users attention in email body. You may modify template as per your need.

3) Now create a email notification which triggers when even is fired as shown in below screenshots.

Go to "System Notification" > "Email" > "Notifications" in left navigation menu and create notification as shown below.

find_real_file.png

In the "Who will receive" tab shown below, "Force Delivery" is not available OOB on form. If you want system to deliver emails irrespective of user preferences for email channel. You may want to add this field and check it. Or you may update this field later from list view.

find_real_file.png

find_real_file.png

 

4) Go to "System Definition" > "Scheduled Jobs" and click "New" and choose "Automatically run a script of your choosing" at the interceptor and create a script which runs periodically as shown below:

find_real_file.png

In the screenshot, I used valid_to > today as I was doing this in my OOB instance which has less data. In your case you may want to to change it as mentioned below:

(function() {
    var kbas = new GlideAggregate("kb_knowledge");
    kbas.addEncodedQuery("workflow_state=published^valid_toRELATIVELT@dayofweek@ahead@30");
    kbas.groupBy("author");
    kbas.query();
    while (kbas.next()) {
        var currAuthor = kbas.getValue("author");
        gs.eventQueue("kb.expiring.articles.email", current, currAuthor, "");
    }
})();

 

Additionally if you want to provide a link in email using which users can directly export the EXCEL file of KB's that needed their attention. You can do this.

First create a new view for kb_knowledge table list alone and give it some name such as "kb_article_export_view", then form the url in below mentioned manner and use this in email template as a link and mention text which clearly directs end user to import the file.

https://<instance_name>/kb_knowledge_list.do?sysparm_query=workflow_state=published^valid_to>javascript:gs.endOfToday()^authorDYNAMIC90d1921e5f510100a9ad2572f2b477fe&sysparm_view=kb_article_export_view&EXCEL

"&EXCEL" is a format processor provided by servicenow using which a given list can be exported directly in excel format.

And the reason for suggesting a new list view creation is, all the end users who try to export always exports the columns which admin or process owner decides.

 

Hope this helps!

 

--

Thanks and regards,

Subrahmanyam Satti