web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Efficient Document Attachment for Custom Table and Page?

(0) ShareShare
ReportReport
Posted on by 485

I have a custom table and page created, and saving a Document Attachment doesn't seem to save the document to the proper record. I have followed this blog to fix this for one of my tables: https://yzhums.com/11177/

This did work for me, but what if I have multiple tables (the article only fixes this for one table "Payment Terms")? Would I have to make a codeunit for each one? I am trying to find the most efficient way of making this dynamic if I have a lot of custom tables.

Thanks!

I have the same question (0)
  • Suggested answer
    JAngle Profile Picture
    159 on at

    The codeunit has an event subscriber to the page and uses a case statement to determine what to do with a specific table. You merely need to add extra tables to the case statement of the same event subscriber procedure.

  • Verified answer
    Brad_P Profile Picture
    1,551 on at

    Hi,

    You can pass the record reference to the Document Attachment page and then subscribe to the events for the document attachment to work with your table. 

    Action to open page:

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

    Assuming single field Primary Key

    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", 'OnAfterInitFieldsFromRecRef', '', true, true)]
    local procedure DocumentAttachment_OnAfterInitFieldsFromRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef)
    var
    FieldRef: FieldRef;
    RecNo: Code[20];
    begin
    case RecRef.Number of
    DATABASE::"Your Table":
    begin
    FieldRef := RecRef.Field(10);
    RecNo := FieldRef.Value;
    DocumentAttachment.Validate("No.", RecNo);
    end;
    end;
    end;

    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Details", 'OnAfterOpenForRecRef', '', true, true)]
    local procedure DocumentAttachmentDetails_OnAfterOpenForRecRef(var RecRef: RecordRef; var DocumentAttachment: Record "Document Attachment"; var FlowFieldsEditable: Boolean)
    var
    FieldRef: FieldRef;
    RecNo: Code[20];
    begin
    case RecRef.Number of
    DATABASE::"Your Table":
    begin
    FieldRef := RecRef.Field(10);
    RecNo := FieldRef.Value;
    DocumentAttachment.SetRange("No.", RecNo);
    FlowFieldsEditable := true;
    end;
    end;
    end;

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,454 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,806 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,538

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans