Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Change Email Attachment File Name

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'.
 
Categories:
  • Suggested answer
    Shrey Chauhan Profile Picture
    Shrey Chauhan 288 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 894 Super User 2024 Season 2 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,620 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,884 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,150

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans