{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":39239,"path":"platform-administration/r_UsefulAttachmentScripts.md","title":"Useful attachment scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# Useful attachment scripts\n\nThis is a searchable version of the Useful Attachment Scripts.\n\n**Warning:** The customization described here was developed for use in specific instances, and is not supported by Now 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](http://community.service-now.com/).\n\n## Copy attachments from record to record\n\nUse the following script to copy an attachment from one record to another record:\n\n```\nGlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');\n```\n\n**Note:** GlideSysAttachment.copy copies all attachments; it cannot select specific attachments.\n\n## Delete duplicate attachments\n\nUse the following script in a business rule, scheduled job, or background script to delete duplicate attachments located in the Attachments \\[sys\\_attachment\\] table:\n\n```\nfunction fixDuplicateImages(){\n  var now_GR = new GlideRecord('sys_attachment');\n  now_GR.addQuery('table_name','LIKE','ZZ_YY%');\n  now_GR.orderBy('table_sys_id');\n  now_GR.orderByDesc('sys_created_on');\n  now_GR.query();\n  var lastID ='not_a_match';\n  var lastFile ='not_a_match';\n  while(now_GR.next()){\n    var isDuplicate = (lastID == now_GR.table_sys_id)&&(lastFile == now_GR.file_name);\n    lastID = now_GR.table_sys_id;\n    lastFile = now_GR.file_name;\n    gs.print(now_GR.table_sys_id + ' ' + now_GR.table_name + ' ' + now_GR.file_name + ' ' + now_GR.sys_created_on + ' ' + isDuplicate);\n    if(isDuplicate)\n      now_GR.deleteRecord();\n  }\n}\n```\n\n## Display whether tasks have attachments in list view\n\nUse 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.\n\n```\ncheckAttachment();\n \nfunction checkAttachment(){\n  // if inserting then the task has an attachment\n  if(current.operation()=='insert') {\n    hasAttachment('true'); // if deleting attachment check for other attachments\n    if(current.operation()=='delete') {\n      var timeNow3 =new GlideDateTime();\n      gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow3.getNumericValue(),'jwtest');\n      var attachCount = new GlideAggregate('sys_attachment');\n      attachCount.addQuery('table_sys_id',current.sys_id);\n      attachCount.addAggregate('COUNT');\n      attachCount.query();\n      var numAttachments ='0'; \n\n      // if no other attachments task does not have attachment\n      if(attachCount.next()){\n        numAttachments = attachCount.getAggregate(\"COUNT\");\n        if(numAttachments >0){\n          hasAttachment ='true';\n        } else {\n          hasAttachment('false');\n        }\n        var timeNow4=new GlideDateTime();\n        gs.log('has_attachment br: gliderecord query start date time is: '+ timeNow4.getNumericValue(),'jwtest');\n      }\n \nfunction hasAttachment(answer){\n  var task = new GlideRecord('task');\n  task.addQuery('sys_id',current.table_sys_id);\n  task.query();\n \n  if(task.next()){\n    task.u_has_attachment= answer;\n    task.autoSysFields(false);  //Don't set the lastUpdatedTime or the Simultaneous Update Alert will likely get triggered\n    task.setWorkflow(false);  //Don't allow other business rules to run, otherwise multiple notifications will likely be sent\n    task.update();\n  }\n}\n```\n\n**Note:** Schedule the business rule to run after insert/delete.\n\n## Link to attachments in an email notification\n\nUse the following script in an email notification or template to include links to attachments:\n\n```\n\nprintattachments(); \n \nfunction printattachments(){\n  var now_GR =new GlideRecord('sys_attachment');\n  now_GR.addQuery('table_sys_id',current.sys_id);\n  now_GR.query();\n  while(now_GR.next()){\n    template.print('Attachment: <a href=\"http://'+gs.getProperty(\"instance_name\")+'.service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'\">'+ now_GR.file_name+'</a>');\n  }\n}\n```\n\n**Note:** Replace \"instance\\_name\" with your instance name.\n\n## Attachment Logging\n\nWhenever 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:\n\n-   parm1: File name\n-   parm2: Table name\n\n**Parent Topic:**[Scripting for email notifications](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/c_ScriptingForEmailNotifications.md)\n\n**Related topics**  \n\n\n[Include mail scripts in email notifications]()\n\n[Mail script variables]()\n\n[Example scripting for email notifications]()\n\n"}}