Useful attachment scripts

This is a searchable version of the Useful Attachment Scripts.

For an easy-to-navigate version, visit the Useful Scripts portal.

Caution: The customization described here was developed for use in specific instances, and is not supported by ServiceNow. This method is provided as-is and should be tested thoroughly before implementation. Post all questions and comments regarding this customization to our community forum.
Table 1. Useful attachment scripts
Name Type Table Description Parameters Script
Copy Attachments from Record to Record
Note: GlideSysAttachment.copy copies all attachments; it cannot select specific attachments.
Delete Duplicate Attachments business rule, scheduled job, background script sys_attachment This script will delete duplicate images located in the attachments table. None
function fixDuplicateImages(){var gr =new GlideRecord('sys_attachment');
    gr.query();var lastID ='not_a_match';var lastFile ='not_a_match';while({var isDuplicate =(lastID == gr.table_sys_id)&&(lastFile == gr.file_name);
        lastID = gr.table_sys_id;
        lastFile = gr.file_name;
        gs.print(gr.table_sys_id+' '+ gr.table_name+' '+ gr.file_name+' '+ gr.sys_created_on+' '+ isDuplicate);if(isDuplicate)
Display Whether Tasks Have Attachments in List View Business Rule Attachments [sys_attachment] Displays whether tasks have attachments when viewed in the record list view. Note that the script needs a custom field on the task table named Has Attachments (u_has_attachments). when: after insert/delete
function checkAttachment(){// if inserting then the task has an attachmentif(current.operation()=='insert'){
    // if deleting attachment check for other attachmentsif(current.operation()=='delete'){var timeNow3 =new GlideDateTime();
		  gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow3.getNumericValue(),'jwtest');var attachCount =new GlideAggregate('sys_attachment');
        var numAttachments ='0';// if no other attachments task does not have attachmentif({
            numAttachments = attachCount.getAggregate("COUNT");if(numAttachments >0){
                hasAttachment ='true';}}else{
            hasAttachment('false');}var timeNow4=new GlideDateTime();
        gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow4.getNumericValue(),'jwtest');}}
function hasAttachment(answer){var task =new GlideRecord('task');
        task.u_has_attachment= answer;
        task.autoSysFields(false);//Don't set the lastUpdatedTime or the Simultaneous Update Alert will likely get triggered
        task.setWorkflow(false);//Don't allow other business rules to run, otherwise multiple notifications will likely be sent
Include Attachments in an Email Notification Include this in the email notification or template to include attachments. Replace INSTANCE with your instance name.
function printattachments(){var gr =new GlideRecord('sys_attachment');
        template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+''+ gr.sys_id+'">'+ gr.file_name+'</a>');}}</mail_script>

Attachment Logging

Whenever a user downloads an attachment, the action writes an event record to the event log. If desired, you can process these events with a Script Action or an Email Notification. This can be useful if you want to do something when an attachment is read. For example, you can record when and by whom certain attachments are downloaded. For this functionality, the current variable must point to a sys_attachment record, and the event record must use the following parameters:
  • parm1: File name
  • parm2: Table name