Unable to rename attachment name in sys_attachment table during the request closure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2024 09:24 PM
Hi All,
I have a requirement to rename the filename of the attachments available for a request when the state of the request moves to closed.
I have written a before insert/update business rule for the request table and added a condition of state changes to closed. The file name is not getting updated. Its still reflecting the old file name. Is it not possible to rename the file name of an attachment once its attached?
function renameAttachments() {
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addEncodedQuery('table_sys_id='+ current.sys_id + '^table_name=' + <table name>);
attachmentGr.query();
while (attachmentGr.next()) {
var originalAttachmentName = attachmentGr.file_name;
var renamedAttachmentName = current.number + ' ' + originalAttachmentName;
attachmentGr.file_name = renamedAttachmentName;
attachmentGr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 11:19 PM
Hi Vrushali,
This is working as I called the update from a global scope. But now the original files are renamed and along with that a new entry is also created with table name as sys_attachment. So two records for the same attachment one with the custom table and new entry with the sys_attachment table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 11:20 PM - edited 07-29-2024 11:53 PM
Hi @Vrushali Kolte , Its working now for me.
You don't need the code to copy the attachment with the new name as it will create a new entry in the table. It was not working for me earlier as the BR is in a scoped application.
Now I created the method to update the filename in the sys_attachment table in global scope and invoked the method from the BR which is in the custom scope and it worked.
Thanks for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 12:36 AM
Please try using the below code.
var tableName = 'incident'; // Table to which the attachment belongs
var recordSysId = 'incident_sys_id_here'; // Sys ID of the record to which the attachment belongs
var oldFileName = 'old_filename.ext'; // Current file name of the attachment
var newFileName = 'new_filename.ext'; // New file name for the attachment
// Query for the attachment with the old file name
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', tableName);
attachmentGR.addQuery('table_sys_id', recordSysId);
attachmentGR.addQuery('file_name', oldFileName);
attachmentGR.query();
if (attachmentGR.next()) {
// Create a GlideSysAttachment object
var attachment = new GlideSysAttachment();
// Download the attachment content
var fileContent = attachment.getContentStream(attachmentGR.sys_id);
// Create the new attachment with the new file name
//GlideSysAttachment - Scoped (servicenow.com)
var newAttachmentId = attachment.write(tableName, recordSysId, newFileName, fileContent);
if (newAttachmentId) {
// Optionally delete the old attachment
attachmentGR.deleteRecord();
gs.info('Attachment renamed and re-uploaded successfully.');
} else {
gs.error('Failed to rename and re-upload the attachment.');
}
} else {
gs.error('No attachment found with the name ' + oldFileName);
}
- Kailas