Dynamics 365 Business Central: How to add Attachments, Links, Notes Factbox to any pages

Dynamics 365 Business Central

Hi, Readers.
Yesterday, we discussed how to calculate and display the Average, Count, Sum of the selected lines in FactBox. For more details: https://yzhums.com/11138/
Today I would like to continue the discussion about FactBox.

As you might know, in the FactBox on most cards and documents, you can attach files, add links, and write notes. For links and notes, you can also do this on the list page by first selecting the related line.

To view or change any of these attached information types, you must first open the Attachments tab in the FactBox. The number behind the tab title indicates how many attached files, links, or notes exist for the card or document.

So for pages that don’t include Attachments, Links, Notes Factbox as standard, or for custom new pages, can we add them ourselves?
For example: Payment Terms

Yes, Let’s try it together.

First you can find the codes of the three Factboxes on the page 21 “Customer Card”.
Document Attachment Factbox is a Common factbox.
Links and Notes Factbox are system factboxes.

Create a page extension to “Payment Terms”, then add three FactBoxes to the page.

pageextension 50111 PaymentTermsExt extends "Payment Terms"
{
    layout
    {
        addfirst(FactBoxes)
        {
            part("Attached Documents"; "Document Attachment Factbox")
            {
                ApplicationArea = All;
                Caption = 'Attachments';
                SubPageLink = "Table ID" = CONST(3),
                              "No." = FIELD(Code);
            }
            systempart(PyamentTermsLinks; Links)
            {
                ApplicationArea = RecordLinks;
            }
            systempart(PyamentTermsNotes; Notes)
            {
                ApplicationArea = Notes;
            }
        }
    }
}

Note: 3 is the table number of Payment Terms, and Code is the primary key of Payment Terms.

If you publish the extension now, the Links and Notes Factboxes are already working properly.

But for Document Attachment Factbox, after adding attachments, you will find that Table ID and No. are not recorded in the table Document Attachment (1173).

Although you can now upload an attachment, that attachment does not point to this record correctly.

Customer Card:

So there are two more steps to do for Document Attachment Factbox.

1. Specifies the open table.

2. Specifies the field associated with the table.

Code:

codeunit 50101 DocumentAttachment
{
    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Factbox", 'OnBeforeDrillDown', '', false, false)]
    local procedure OnBeforeDrillDown(DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef);
    var
        PaymentTerms: Record "Payment Terms";
    begin
        case DocumentAttachment."Table ID" of
            DATABASE::"Payment Terms":
                begin
                    RecRef.Open(DATABASE::"Payment Terms");
                    if PaymentTerms.Get(DocumentAttachment."No.") then
                        RecRef.GetTable(PaymentTerms);
                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::"Payment Terms":
                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::"Payment Terms":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.Validate("No.", RecNo);
                end;
        end;
    end;
}

Ok, publish extension again and test.

Test Video:

Update for the case of multiple primary keys:

pageextension 50117 VendorBankAccountCard extends "Vendor Bank Account Card"
{
    actions
    {
        addfirst(Processing)
        {
            action(DocAttach)
            {
                ApplicationArea = All;
                Caption = 'Attachments';
                Image = Attach;
                ToolTip = 'Add a file as an attachment. You can attach images as well as documents.';

                trigger OnAction()
                var
                    DocumentAttachmentDetails: Page "Document Attachment Details";
                    RecRef: RecordRef;
                begin
                    RecRef.GetTable(Rec);
                    DocumentAttachmentDetails.OpenForRecRef(RecRef);
                    DocumentAttachmentDetails.RunModal();
                end;
            }
        }
    }
}

codeunit 50117 DocumentAttachment2
{
    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Factbox", 'OnBeforeDrillDown', '', false, false)]
    local procedure OnBeforeDrillDown(DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef);
    var
        VendorBankAccount: Record "Vendor Bank Account";
    begin
        case DocumentAttachment."Table ID" of
            DATABASE::"Vendor Bank Account":
                begin
                    RecRef.Open(DATABASE::"Vendor Bank Account");
                    if VendorBankAccount.Get(DocumentAttachment."No.") then
                        RecRef.GetTable(VendorBankAccount);
                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 FlowFieldsEditable: Boolean);
    var
        FieldRef: FieldRef;
        RecNo: Code[20];
        VendorBankCode: Code[20];
    begin
        case RecRef.Number of
            DATABASE::"Vendor Bank Account":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.SetRange("No.", RecNo);

                    FieldRef := RecRef.Field(2);
                    VendorBankCode := FieldRef.Value;
                    DocumentAttachment.SetRange("Vendor Bank Number", VendorBankCode);

                    FlowFieldsEditable := false;
                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];
        VendorBankCode: Code[20];
    begin
        case RecRef.Number of
            DATABASE::"Vendor Bank Account":
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.Validate("No.", RecNo);

                    FieldRef := RecRef.Field(2);
                    VendorBankCode := FieldRef.Value;
                    DocumentAttachment.Validate("Vendor Bank Number", VendorBankCode);
                end;
        end;
    end;
}

tableextension 50100 DocumentAttachmentExt extends "Document Attachment"
{
    fields
    {
        field(50100; "Vendor Bank Number"; Code[20])
        {
            DataClassification = CustomerContent;
        }
    }
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL