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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Technically it should be possible to change the file_name of an attachment.

 

Have you verified the Business Rule is triggered at all? That it starts executing your code? That the GlideRecord query does return records? Add some debugging to your script, and you will find the issue within seconds.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Just tested your code (against incident table), works fine. So did your business rule actually trigger? Are your triggers correct?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Vrushali  Kolte
Mega Sage

Hello @janindiadoc_1 ,

 

Try below script, it should resolve your issue. I have tried it in my PDI and it worked for me.

Please make changes as per your query.

 

You need to make use of GlideAttachmentAPI in order to rename the file name.

 

 

function renameAttachments() {
    var attachmentGr = new GlideRecord('sys_attachment');
   // attachmentGr.addQuery('table_sys_id', '66b8e8499313021087c03a6efaba10a3');
    attachmentGr.addEncodedQuery('table_sys_id='+ current.sys_id + '^table_name=' + <table name>);
    attachmentGr.query();
    
    while(attachmentGr.next()) {
        var originalAttachmentName = attachmentGr.file_name.toString();
        var renamedAttachmentName = 'Vrushali'+ ' ' + originalAttachmentName;
        
        // Copy the attachment with the new name
        var attachment = new GlideSysAttachment();
        var attachmentContent = attachment.getBytes(attachmentGr);
        attachment.write(attachmentGr, renamedAttachmentName, attachmentGr.content_type, attachmentContent);

        // Update the original attachment record to point to the new file name
        attachmentGr.file_name = renamedAttachmentName;
        attachmentGr.update();
    }
}

 

 

VrushaliKolte_0-1722228586778.png

 

If my answer solves your issue please mark it as Accepted ✔️& Helpful👍!

 

This is not working for me. Is it because that I'm in a scoped application. Only read cross scope privilege is created.