Unable to rename attachment name in sys_attachment table during the request closure

janindiadoc_1
Tera Expert

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();

        }

    }

 

7 REPLIES 7

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.

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.

KB18
Tera Guru
Tera Guru

@janindiadoc_1 

 

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);
}

Please hit the thumb Icon and mark as correct in case I help you with your query!!!
- Kailas