Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Change Email Attachment File Name

Posted on by 3,810
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 255 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;

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans