How to translate attachment names

The event subscribers mentioned in this article were tested with Business Central 2023 release wave 1 (BC22). We cannot guarantee the correct working on different Business Central versions, as these event subscribers might change between versions.

When Business Central generates email attachment filenames, it uses a hardcoded way of generating them. For Business Central 2023 release wave 1 (BC22), most of these attachment names are translated. Some, however, are not. Most notable among these is the statement. The statement uses different code for email generation where the translation has not been implemented.

Fortunately, you can manually override the translations using an event subscriber.

To translate the statement, you can use the OnGenerateFileNameOnAfterAssignFileName event in the Custom Layout Reporting codeunit. The following is sample code using the ForNAV Language Setup table.

[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);
        ForNAVLanguageSetup.SetRange("Report ID", ReportID);
        ForNAVLanguageSetup.SetRange("Language Code", Customer."Language Code");
        ForNAVLanguageSetup.SetRange("Table No.", 0);
        ForNAVLanguageSetup.SetRange("Field No.", 0);
        if not ForNAVLanguageSetup.FindFirst() then begin
            AllObjWithCaption.Get(AllObjWithCaption."Object Type"::Report, ReportID);
            FileName := StrSubstNo(FileNameLbl, AllObjWithCaption."Object Caption", Customer.Name, Extension);
            exit;
        end;

        FileName := StrSubstNo(FileNameLbl, ForNAVLanguageSetup."Translate To", Customer.Name, Extension);
    end;


To override any attachment filename, you can use the OnBeforeGetAttachmentFileName event in the Document-Mailing codeunit. Technically, you can override the statement attachment name here as well, however, there is no reliable way of getting the statement language from this event. It works well for sales and purchase documents though. The following is an example:

[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 "ForNAV Language Setup";
        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);
                    ForNAVLanguageSetup.SetRange("Report ID", 0);
                    ForNAVLanguageSetup.SetRange("Language Code", SalesInvoiceHeader."Language Code");
                    ForNAVLanguageSetup.SetRange("Table No.", Database::"Sales Invoice Header");
                    ForNAVLanguageSetup.SetRange("Field No.", 0);
                    if not ForNAVLanguageSetup.FindFirst() then
                        exit;

                    AttachmentFileName := StrSubstNo(ReportAsPdfFileNameMsg, ForNAVLanguageSetup."Translate To", PostedDocNo)
                end;
        end;
    end;


You can see the full code in the ForNAV GitHub repository here:

https://github.com/fornav/ForNav.Training/tree/master/ForNAV.ChangeAttachmentName