see who has downloaded the attachment

Parul Maheshwar
Tera Contributor

Hi All ,

We have a requirement in which we want to see who has downloaded the attachment.Please find the code below :

Also please note the table on which we want to see who has downloaded the attachment is in scoped app and is extended from task,weird thing is we can see all the logs in system logs it means our business rule is working fine ,

FYI : we are trying to update the work notes field on the custom app table with the name of the user who has downloaded the attachment .

The business rule is on "after insert"

(function executeRule(current, previous /*null when async*/) {

var gr1=new GlideRecord('sysevent');
gr1.addQuery('name','attachment.read');
gr1.addQuery('parm2',' table name ');//name of the table
gr1.orderByDesc('sys_created_on');
gr1.setLimit(1);
gr1.query();
gs.info('rows1 '+gr1.getRowCount()); /////////////////////////

if(gr1.next())
{
var gr2=new GlideRecord('sys_attachment');
gr2.addQuery('table_name','table name');//name of the table 
gr2.addQuery('file_name',gr1.parm1);
gr2.query();
gs.info('rows2 '+gr2.getRowCount()); ////////////////////////

if(gr2.next())
{
var gr=new GlideRecord('table name');
gr.addQuery('sys_id',gr2.table_sys_id);
gr.query();
gs.info('rows1=3--'+gr.getRowCount()); //////////////////////////////////

if(gr.next())
{
var date=new GlideDateTime();
var work_notes='Attachment is downloaded by '+ 'Random123' +' at '+date.getDisplayValue();
gs.info(work_notes); //////////////////////////////////
gr.work_notes=work_notes;

gr.update();
}}}

})(current, previous);

3 REPLIES 3

Tony Chatfield1
Kilo Patron
Hi, OOB there is a sysevent created at each attachment download, I think attachment.read. parameters include file name and table, the user identified via sys_created_by. I think you should be able to utilise the event and/or underpinning code in your objective

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you try checking the log statements?

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Tony Chatfield1
Kilo Patron

Sorry I deleted my first response as I didn't read your post fully.

You indicate you want to identify the user who 'downloaded' the attachment, but are running your BR script on an on after insert - this trigger is unlikely to have any correlation with a user downloading an attachment file from a task\record.

The assumption is also being made that there will be only 1 attachment with a matching name per table which is very unlikely, also sorting results of busy tables on last updated by in order to retrieve the correct record is questionable, and I don't see this intended solution as viable.

 

I am reluctant to suggest running code against core system tables, but the field you need is the sysevent.instance as this identifies the sys_id of the sys_attachment record, which then gives you the record table and sys_id. Attachment.read also gives you the user if you look up sys_user.user_name for attachment.read's sys_created_by

BUT perhaps some further consideration should be given as I don't see this as ideal and believe that you should exhaust all other possibilities before considering it.