Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Change Email Attachment File Name

(1) ShareShare
ReportReport
Posted on by 3,893
How can I change the default naming format of the Email Attachment file name. For example the default file name below is 'Sales Quote S-QUO1001.pdf'. I would like to change the default Name to include Customer Name for example 'Trey Research S-QUO1001.pdf'.
 
  • Suggested answer
    Shrey Chauhan Profile Picture
    276 on at
    Change Email Attachment File Name
    Hello,

    Here's the Code to change the Attachment File Name :
     
    codeunit 50008 "Attachment FileName"
    {
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeGetAttachmentFileName', '', false, false)]
        local procedure OnBeforeGetAttachmentFileName(var AttachmentFileName: Text[250]; PostedDocNo: Code[20]; EmailDocumentName: Text[250]; ReportUsage: Integer)
        var
            SalesInvoiceHeader: Record "Sales Invoice Header";
            ForNAVLanguageSetup: Record "Email Attachments";
            ReportSelectionUsage: Enum "Report Selection Usage";
            ReportAsPdfFileNameMsg: Label '%1 %2.pdf', Comment = '%1 = Document Type %2 = Invoice No. or Job Number';
        begin
            if PostedDocNo = '' then
                exit;
            case ReportUsage of
                ReportSelectionUsage::"S.Invoice".AsInteger():
                    begin
                        SalesInvoiceHeader.Get(PostedDocNo);
                AttachmentFileName := "Custom Name"
                    end;
            end;
        end;
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Custom Layout Reporting", 'OnGenerateFileNameOnAfterAssignFileName', '', false, false)]
        local procedure OnGenerateFileNameOnAfterAssignFileName(var FileName: Text; ReportID: Integer; Extension: Text; DataRecRef: RecordRef)
        var
            AllObjWithCaption: Record AllObjWithCaption;
            Customer: Record Customer;
            ForNAVLanguageSetup: Record "ForNAV Language Setup";
            FilenameLbl: Label '%1 - %2%3', Comment = '%1 = report caption, %2 = customer name, %3 = extension';
        begin
            if DataRecRef.Number <> Database::Customer then
                exit;
            DataRecRef.SetTable(Customer);
           
            FileName := "Custom Name";
        end;
    }

    Hope this Solution's Help.

    Thank You,
    Shrey Chauhan
  • Suggested answer
    Yi Yong Profile Picture
    2,153 Super User 2025 Season 1 on at
    Change Email Attachment File Name
    Hello DinB,
     
    You can do it via customize and subscribe to the below event.
     
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Document-Mailing", 'OnBeforeGetAttachmentFileName', '', false, false)]

    local procedure OnBeforeGetAttachmentFileName(var AttachmentFileName: Text[250]; PostedDocNo: Code[20]; EmailDocumentName: Text[250]; ReportUsage: Integer);
    begin
    // Use 'ReportUsage' for different sending events.
    // For example ReportUsage = 2 is for Posted Sales Invoice.
    end;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2,655

#2
Mansi Soni Profile Picture

Mansi Soni 1,574

#3
YUN ZHU Profile Picture

YUN ZHU 1,453 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans