Useful attachment scripts
Summarize
Summary of Useful Attachment Scripts
This guide provides practical scripts for managing attachments within ServiceNow instances. These scripts enable customers to automate common attachment-related tasks such as copying attachments between records, deleting duplicates, displaying attachment indicators in task lists, embedding attachment links in email notifications, and logging attachment downloads. Note that these scripts are customizations developed for specific scenarios and are not officially supported by Now Support. Thorough testing in your environment is recommended before implementation.
Show less
Attachment Management Scripts
- Copy Attachments: Use
GlideSysAttachment.copyto copy all attachments from one record to another. This copies every attachment without filtering for specific files. - Delete Duplicate Attachments: A script can identify and delete duplicate attachments based on table and file name by querying the
sysattachmenttable, useful for cleanup in scheduled jobs or business rules. - Display Attachment Presence in List View: Implement a business rule that updates a custom field
uhasattachmentson task records to indicate whether attachments exist. This requires running the rule after attachment insertions or deletions and prevents triggering unnecessary updates or notifications.
Email and Logging Enhancements
- Include Attachment Links in Email Notifications: Use a script in email templates to dynamically generate clickable links to attachments associated with a record by querying the
sysattachmenttable. Remember to replace the instance name placeholder with your actual instance URL. - Attachment Download Logging: Attachment downloads trigger
attachment.readevents that can be captured via Script Actions or Email Notifications. This feature allows tracking who accessed attachments and when, aiding in auditing and security monitoring.
Practical Considerations
These scripts empower ServiceNow customers to enhance attachment handling and visibility with automation and customization tailored to their workflows. Customers should ensure they have the necessary custom fields (e.g., uhasattachments) and schedule business rules appropriately. Since these scripts are provided "as-is," thorough testing and validation in non-production environments are critical to avoid unintended impacts.
This is a searchable version of the Useful Attachment Scripts.
Copy attachments from record to record
GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');
Delete duplicate attachments
function fixDuplicateImages(){
var now_GR = new GlideRecord('sys_attachment');
now_GR.addQuery('table_name','LIKE','ZZ_YY%');
now_GR.orderBy('table_sys_id');
now_GR.orderByDesc('sys_created_on');
now_GR.query();
var lastID ='not_a_match';
var lastFile ='not_a_match';
while(now_GR.next()){
var isDuplicate = (lastID == now_GR.table_sys_id)&&(lastFile == now_GR.file_name);
lastID = now_GR.table_sys_id;
lastFile = now_GR.file_name;
gs.print(now_GR.table_sys_id + ' ' + now_GR.table_name + ' ' + now_GR.file_name + ' ' + now_GR.sys_created_on + ' ' + isDuplicate);
if(isDuplicate)
now_GR.deleteRecord();
}
}Display whether tasks have attachments in list view
checkAttachment();
function checkAttachment(){
// if inserting then the task has an attachment
if(current.operation()=='insert') {
hasAttachment('true'); // if deleting attachment check for other attachments
if(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 attachment
if(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();
}
}
Link to attachments in an email notification
printattachments();
function printattachments(){
var now_GR =new GlideRecord('sys_attachment');
now_GR.addQuery('table_sys_id',current.sys_id);
now_GR.query();
while(now_GR.next()){
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>');
}
}
Attachment Logging
- parm1: File name
- parm2: Table name