Dynamics 365 Business Central: Automatically attaching as PDF to posted sales invoice (Document Attachment)

Dynamics 365 Business Central

Hi, Readers.
I saw a question in community yesterday, is there a way to automatically attach a PDF to a posted sales invoice? He wants to save the report before posting in the posted sales invoice.

In fact, BC has standard feature (Attach as PDF…) that can do this, however, it needs to be manually clicked once.

For example:

Okay, how do we make it automated? It’s not very difficult, and there is more than one method, I prefer to use the SaveAsDocumentAttachment in table 77 “Report Selections”.

Because using this method can flexibly switch various “Report Selection Usage”.

Standard Source Code:

    procedure SaveAsDocumentAttachment(ReportUsage: Integer; RecordVariant: Variant; DocumentNo: Code[20]; AccountNo: Code[20]; ShowNotificationAction: Boolean)
    var
        TempAttachReportSelections: Record "Report Selections" temporary;
        DocumentAttachmentMgmt: Codeunit "Document Attachment Mgmt";
        TempBlob: Codeunit "Temp Blob";
        RecRef: RecordRef;
        NumberOfReportsAttached: Integer;
        IsHandled: Boolean;
    begin
        RecRef.GETTABLE(RecordVariant);
        if not RecRef.Find() then
            exit;

        FindPrintUsageInternal(
            "Report Selection Usage".FromInteger(ReportUsage), AccountNo, TempAttachReportSelections, GetAccountTableId(RecRef.Number()));
        with TempAttachReportSelections do
            repeat
                if CanSaveReportAsPDF(TempAttachReportSelections."Report ID") then begin
                    Clear(TempBlob);
                    SaveReportAsPDFInTempBlob(TempBlob, "Report ID", RecordVariant, "Custom Report Layout Code", "Report Selection Usage".FromInteger(ReportUsage));
                    SaveDocumentAttachmentFromRecRef(RecRef, TempAttachReportSelections, DocumentNo, AccountNo, TempBlob, NumberOfReportsAttached);
                end;
            until Next() = 0;

        IsHandled := false;
        OnSaveAsDocumentAttachmentOnBeforeShowNotification(RecordVariant, NumberOfReportsAttached, ShowNotificationAction, IsHandled);
        if not IsHandled then
            DocumentAttachmentMgmt.ShowNotification(RecordVariant, NumberOfReportsAttached, ShowNotificationAction)
    end;

Let’s look at two examples. I added the program to OnBeforeAction() trigger of actions for testing, you can add to where you need it.

1. In Sales Orders

Source Code:

pageextension 50108 SalesOrderExt extends "Sales Order"
{
    actions
    {
        modify(Post)
        {
            trigger OnBeforeAction()
            var
                ReportSelections: Record "Report Selections";
                Usage: Enum "Report Selection Usage";
                SalesHeader: Record "Sales Header";
            begin
                SalesHeader := Rec;
                SalesHeader.SetRecFilter();
                ReportSelections.SaveAsDocumentAttachment(
                            Usage::"S.Order".AsInteger(), SalesHeader, SalesHeader."No.", SalesHeader.GetBillToNo(), false);
            end;
        }
    }
}

Test Video:

2. In Sales Invoice

Source Code:

pageextension 50109 SalesInvoiceExt extends "Sales Invoice"
{
    actions
    {
        modify(PostAndSend)
        {
            trigger OnBeforeAction()
            var
                ReportSelections: Record "Report Selections";
                Usage: Enum "Report Selection Usage";
                SalesHeader: Record "Sales Header";
            begin
                SalesHeader := Rec;
                SalesHeader.SetRecFilter();
                ReportSelections.SaveAsDocumentAttachment(
                            Usage::"Pro Forma S. Invoice".AsInteger(), SalesHeader, SalesHeader."No.", SalesHeader.GetBillToNo(), false);
            end;
        }
    }
}

Test Video:

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました