Create a notification when reaching 20 download attempts

MStritt
Tera Guru

I'm looking to create a notification, when 20 download attempts (knowledge articles) are made by any logged in user (Customer Support Portal) during a 24 hour period. How/where would I run a query to provide/show that information? And, tie it to a notification?

5 REPLIES 5

AshishKM
Kilo Patron
Kilo Patron

Hi @MStritt , 

The main part , how to count the download ( or attempts ) for this notification else you can apply required filter and trigger the notification. 

The kb_use table count the view but not the download. 

 

https://www.servicenow.com/community/developer-forum/is-there-a-table-to-check-the-number-of-downloa...

 

In the below thread Jaspal Singh has given an idea but that from system log tables, you can check.

https://www.servicenow.com/community/knowledge-managers/is-there-a-view-download-count-for-attachmen... 

 

-Thanks,

AshishKMishra

Please accept solution and mark helpful for others if it helps you


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Tai Vu
Kilo Patron
Kilo Patron

Hi @MStritt 

Let's try the below approach.

1. Create an Event Registry in the Knowledge [kb_knowledge] for the Notification.

2. Create a Script Action triggered when the event attachment.read fired.

3. Add condition only runs when table is kb_knowledge. Then calculate the total event in the day to verify if it reaches 20 times.

Sample below.

TaiVu_0-1699416672313.png

 

 

var date_query = "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";

var gaEvent = new GlideAggregate('sysevent');
gaEvent.addQuery('name', 'attachment.read');
gaEvent.addQuery('table', current.getTableName());
gaEvent.addQuery('instance', current.getUniqueValue());
gaEvent.addEncodedQuery(date_query);
gaEvent.groupBy('instance');
gaEvent.addAggregate('COUNT', 'sys_id');
gaEvent.query();
if(gaEvent.next()){
	var total =  parseInt(gaEvent.getAggregate('COUNT', 'sys_id'));
	if(total === 20){
		var grKnowledge = new GlideRecord('kb_knowledge');
		if(grKnowledge.get(current.table_sys_id)){
			gs.eventQueue('<your_notify_event_name>', grKnowledge); //Replace your event name
		}
	}
}

 

 

Cheers,

Tai Vu

Hi @Tai Vu ,

Thanks for sharing code here , does this code count the total downloads or just read.  

 

-Thanks


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi @AshishKM 

The approach above is referenced from your comment.

Screenshot 2023-11-08 at 11.46.19.png

 

One observation I've made is that when we click on the attachment, it opens the following URL.

URL: https://<instance_name>/sys_attachment.do?sys_id=<the_sys_id_attachment_record>

Following this, the event attachment.read gets triggered.

 

In scenarios where the instance utilizes the Document Viewer in the Knowledge [kb_knowledge] table, we may need to explore another way to identify when users open the Viewer or download the content.

 

Cheers,

Tai Vu