Blog

Attachment changes in Audit Log

Here is a code change that will log attachment changes in the activities section of a record.

This is written to record attachment changes in certain task records.  However you could change it to also work with other records as well.

Dictionary Entry

Table: Task [task]
Type: Journal Input
Column Label: Attachment
Column name: u_attachment_changes

Personalize Activities

  1. Open existing Incident
  2. Right click Activities > Personalize
  3. Add Attachment changes field
  4. click Save

Business Rule: Attachment audit

Table: Attachment [sys_attachment]
Insert: true
Delete: true
When: before
Condition: 
Script:
if (current.table_name == 'incident' || current.table_name == 'change_request' || current.table_name == 'problem' || current.table_name == 'problem_task' || current.table_name == 'sc_task' || current.table_name == 'change_task' || current.table_name == 'sc_request' || current.table_name == 'sc_req_item' ) {
    addAttachmentAudit();
}
function addAttachmentAudit() {
    var attachmentMsg = '';
    var gr = new GlideRecord(current.table_name);
    if (gr.get(current.table_sys_id)) {
        if (current.operation() == 'delete') {
            attachmentMsg = 'Attachment deleted: '+current.file_name+' by ' +current.sys_created_by+' on '+gs.nowDateTime();
        }
        else {
            attachmentMsg = 'Attachment added: '+current.file_name+' by ' +current.sys_created_by;
        }
        gr.u_attachment_changes=attachmentMsg;
        gr.update();
    }
}

Business Rule: EMAIL Attachment audit

Table: Email [sys_email]
When: after 
Insert: true
Update: true
Condtion: !current.instance.nil() && current.type == 'received'
Script:
addAttachmentAudit();
function addAttachmentAudit() {
    var instance = current.instance;
    var targetTable = current.target_table;
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_sys_id',current.sys_id);
    grAttachment.query();
    while(grAttachment.next()){
        grAttachment.table_name = targetTable;
        grAttachment.table_sys_id = instance;
        grAttachment.update();
        var grTargetTable = new GlideRecord(targetTable);
        grTargetTable.addQuery('sys_id',instance);
        grTargetTable.query();
        while (grTargetTable.next()) { 
            grTargetTable.work_notes = "Attachment added: " + grAttachment.file_name + " by email from " + current.sys_created_by;
            grTargetTable.update();
        }
    } 
}

Screenshots

Attachments Added (Click to view)

Attachments Deleted (Click to view)