Dynamics 365 Business Central: How to copy attachments from Warehouse Receipt to Posted Warehouse Receipt – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I would like to discuss a custom solution about Business Central, how to copy attachments from Warehouse Receipt to Posted Warehouse Receipt.

I was asked this question yesterday, it’s not very difficult, so in this post I want to share it briefly, if you encounter similar requirement, I hope it can give you some hints.

On most list pages, cards, and documents, you can attach files, add links, and write notes on the Attachments tab of the FactBox pane. The number in the tab title indicates how many attached files, links, or notes exist for the card or document.

But for some warehouse documents, such as Warehouse Receipt and Posted Warehouse Receipt, there is no such function.

Warehouse Receipt:

Posted Warehouse Receipt:

In the first step, we must add the Attachments feature for the required page. We discussed it in How to add Attachments, Links, Notes Factbox to any pages before, so I won’t repeat the explanation this time.

Page:

Codeunit:

Okay, let’s test it briefly.

Warehouse Receipt:

Posted Warehouse Receipt:

Looks great. The next task is to copy the attachments from Warehouse Receipt to Posted Warehouse Receipt when posting.

The event we can use this time is OnAfterPostedWhseRcptHeaderInsert. Its variables contain “Warehouse Receipt Header” and “Posted Whse. Receipt Header”.

If you need a reference, you can find procedure CopyAttachments in codeunit 1173 “Document Attachment Mgmt”. But the standard takes into account all the master data and sales/purchase documents, so RecordRef Data Type is used, and we don’t have to be so complicated this time.

Here is my test codes.

A small point to note is that there is a “Document Type” field in the “Document Attachment” table, if you do not assign a value, the default is 0 (“Quote”). If you want to distinguish, you can add a new enum value.

Test Video: Looks fine.😁

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs and for the convenience of copying, I have concentrated the code in an AL file, please manage it separately in your project.)

pageextension 50111 WarehouseReceiptExt extends "Warehouse Receipts"
{
    layout
    {
        addfirst(FactBoxes)
        {
            part("Attached Documents"; "Document Attachment Factbox")
            {
                ApplicationArea = All;
                Caption = 'Attachments';
                SubPageLink = "Table ID" = CONST(Database::"Warehouse Receipt Header"),
                              "No." = FIELD("No.");
            }
        }
    }
}

pageextension 50113 PostedWhseReceiptExt extends "Posted Whse. Receipt List"
{
    layout
    {
        addfirst(FactBoxes)
        {
            part("Attached Documents"; "Document Attachment Factbox")
            {
                ApplicationArea = All;
                Caption = 'Attachments';
                SubPageLink = "Table ID" = CONST(Database::"Posted Whse. Receipt Header"),
                              "No." = FIELD("No.");
            }
        }
    }
}

enumextension 50111 AttachmentDocumentType extends "Attachment Document Type"
{
    value(50010; "Warehouse Receipt")
    {
        Caption = 'Warehouse Receipt';
    }
}

codeunit 50113 DocumentAttachment
{
    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Factbox", 'OnBeforeDrillDown', '', false, false)]
    local procedure OnBeforeDrillDown(DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef);
    var
        WarehouseReceiptHeader: Record "Warehouse Receipt Header";
        PostedWhseReceiptHeader: Record "Posted Whse. Receipt Header";
    begin
        case DocumentAttachment."Table ID" of
            DATABASE::"Warehouse Receipt Header":
                begin
                    RecRef.Open(DATABASE::"Warehouse Receipt Header");
                    if WarehouseReceiptHeader.Get(DocumentAttachment."No.") then
                        RecRef.GetTable(WarehouseReceiptHeader);
                end;
            Database::"Posted Whse. Receipt Header":
                begin
                    RecRef.Open(DATABASE::"Posted Whse. Receipt Header");
                    if PostedWhseReceiptHeader.Get(DocumentAttachment."No.") then
                        RecRef.GetTable(PostedWhseReceiptHeader);
                end;
        end;
    end;

    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Details", 'OnAfterOpenForRecRef', '', false, false)]
    local procedure OnAfterOpenForRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef);
    var
        FieldRef: FieldRef;
        RecNo: Code[20];
    begin
        case RecRef.Number of
            DATABASE::"Warehouse Receipt Header":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.SetRange("No.", RecNo);
                end;
            DATABASE::"Posted Whse. Receipt Header":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.SetRange("No.", RecNo);
                end;
        end;
    end;

    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnAfterInitFieldsFromRecRef', '', false, false)]
    local procedure OnAfterInitFieldsFromRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef)
    var
        FieldRef: FieldRef;
        RecNo: Code[20];
    begin
        case RecRef.Number of
            DATABASE::"Warehouse Receipt Header":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.Validate("No.", RecNo);
                end;
            DATABASE::"Posted Whse. Receipt Header":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.Validate("No.", RecNo);
                end;
        end;
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Whse.-Post Receipt", OnAfterPostedWhseRcptHeaderInsert, '', false, false)]
    local procedure CopyAttachments(var PostedWhseReceiptHeader: Record "Posted Whse. Receipt Header"; WarehouseReceiptHeader: Record "Warehouse Receipt Header");
    var
        FromDocumentAttachment: Record "Document Attachment";
        ToDocumentAttachment: Record "Document Attachment";
    begin
        FromDocumentAttachment.SetRange("Table ID", Database::"Warehouse Receipt Header");
        if FromDocumentAttachment.IsEmpty() then
            exit;

        FromDocumentAttachment.SetRange("No.", WarehouseReceiptHeader."No.");
        if FromDocumentAttachment.FindSet() then
            repeat
                Clear(ToDocumentAttachment);
                ToDocumentAttachment.Init();
                ToDocumentAttachment.TransferFields(FromDocumentAttachment);
                ToDocumentAttachment.Validate("Table ID", Database::"Posted Whse. Receipt Header");
                ToDocumentAttachment.Validate("No.", PostedWhseReceiptHeader."No.");
                ToDocumentAttachment.Validate("Document Type", Enum::"Attachment Document Type"::"Warehouse Receipt");
                if not ToDocumentAttachment.Insert(true) then;
                ToDocumentAttachment."Attached Date" := FromDocumentAttachment."Attached Date";
                ToDocumentAttachment.Modify();
            until FromDocumentAttachment.Next() = 0;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL