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
GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');
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.addQuery('table_name','LIKE','ZZ_YY%');
    gr.orderBy('table_sys_id');
    gr.orderByDesc('sys_created_on');
    gr.query();var lastID ='not_a_match';var lastFile ='not_a_match';while(gr.next()){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)
            gr.deleteRecord();}}
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
checkAttachment();
 
function checkAttachment(){// if inserting then the task has an attachmentif(current.operation()=='insert'){
        hasAttachment('true');}
 
    // 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');
        attachCount.addQuery('table_sys_id',current.sys_id);
        attachCount.addAggregate('COUNT');
        attachCount.query();
 
        var numAttachments ='0';// if no other attachments task does not have attachmentif(attachCount.next()){
            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.addQuery('sys_id',current.table_sys_id);
    task.query();
 
    if(task.next()){
        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
        task.update();}}
Include Attachments in an Email Notification Include this in the email notification or template to include attachments. Replace INSTANCE with your instance name.
<mail_script>
 
printattachments(); 
 
function printattachments(){var gr =new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id',current.sys_id);
    gr.query();while(gr.next()){
        template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='+ gr.sys_id+'">'+ gr.file_name+'</a>');}}</mail_script>

Attachment Logging

Whenever a user downloads an attachment, the action writes an attachment.read 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