Thank you for your feedback.
Form temporarily unavailable. Please try again or contact to submit your comments.
  • Madrid
  • London
  • Kingston
  • Jakarta
  • Istanbul
  • Helsinki
  • Geneva
  • Store

Useful attachment scripts

Log in to subscribe to topics and get notified when content changes.

Useful attachment scripts

This is a searchable version of the Useful Attachment Scripts.

Caution: The customization described here was developed for use in specific instances, and is not supported by ServiceNow Customer Support. 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.

Copy attachments from record to record

Use the following script to copy an attachment from one record to another record:
Note: GlideSysAttachment.copy copies all attachments; it cannot select specific attachments.

Delete duplicate attachments

Use the following script in a business rule, scheduled job, or background script to delete duplicate attachments located in the Attachments [sys_attachment] table:
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

Use the following script in a business rule to display whether tasks have attachments when viewed in the record list view. Note that the script needs a custom field on the Has Attachments [u_has_attachments] table.
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');
        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
Note: Schedule the business rule to run after insert/delete.

Include attachments in an email notification

Use the following script in an email notification or template to include attachments:
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>');}}
Note: Replace "instance_name" with your instance name.

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