Skip to main content

Notifications

Announcements

No record found.

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,812
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
    Shrey Chauhan 272 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
    Yi Yong 1,859 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans