Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Rename file attached to the notification when user downloads the file from the email notification

oww15
Tera Contributor

Hi! How can I achieve renaming a file attached to the notification once downloaded? I want to achieve something like ---> user downloads attached file from the notification ----> the requester's name will be added in the beginning of the file. It is displayed correctly in the email body, but after downloading, it displays the original file name. Any idea?

 

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@oww15 

if user receives email with the attachment then they are doing the action in their outlook or email application which is outside ServiceNow application.

how would ServiceNow be able to handle this -> No this is not possible.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@oww15 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @oww15 

the email client (like Outlook) will always show the file name that was sent in the email's headers, not the display name in the email body,

Give a try to Before Insert Business Rule on sys_email

(function executeRule(current, previous /*null when async*/) {

    var grSource = new GlideRecord(current.target_table);
    if (!grSource.get(current.instance)) {
        gs.warn('Email attachment renamer: Could not find source record ' + current.instance);
        return;
    }
    var requesterName = grSource.getDisplayValue('requested_for'); 
    if (gs.nil(requesterName)) {
        requesterName = 'User'; 
    }
    
    // Find all attachments that have been copied to THIS email record
    var grAttach = new GlideRecord('sys_attachment');
    grAttach.addQuery('table_name', 'sys_email');
    grAttach.addQuery('table_sys_id', current.getUniqueValue());
    grAttach.query();

    // Loop through each attachment and rename it
    while (grAttach.next()) {
        var originalName = grAttach.getValue('file_name');
        
        // This is your new file name format
        var newName = requesterName + '_' + originalName;

    
        grAttach.setValue('file_name', newName);
        grAttach.update();
    }

})(current, previous);

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma