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

Community site session details

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

Document Attachment not updating when ID is changed

(2) ShareShare
ReportReport
Posted on by 109

I have a custom page that includes a Document Attachment FactBox.

 

The Document Attachment FactBox is working as expected.

 

However, if I change the 'Id' field of the record, the document files are not being attached to the new 'Id'.

 

If I change the 'Id' back to the old value, the documents reappear.

 

In standard Business Central pages, for example, the 'Project' Card page, when the 'No.' field is updated, the document attachments are correctly linked to the new 'No.'.


How can I make my custom page update the document attachments to follow the new 'Id'?


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
I have the same question (0)
  • Suggested answer
    Suresh Kulla Profile Picture
    49,257 Super User 2025 Season 2 on at
    Document Attachment not updating when ID is changed
    As @Chavda Dharmendra mentioned, you need to subscribe to the OnRename event to move attachments from one record to another.
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    19,036 Super User 2025 Season 2 on at
    Document Attachment not updating when ID is changed
     
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    19,036 Super User 2025 Season 2 on at
    Document Attachment not updating when ID is changed
    I have checked with the below code and i think its working as expected.
    codeunit 50110 "Doc Attachment Handlers"
    {
        [EventSubscriber(ObjectType::Page, Page::"Doc. Attachment List Factbox",
                         'OnAfterGetRecRefFail', '', false, false)]
        local procedure DocAttachmentListFactbox_OnAfterGetRecRefFail(
            var Sender: Page "Doc. Attachment List Factbox";
            DocumentAttachment: Record "Document Attachment";
            var RecRef: RecordRef)
        var
            TestTable: Record "Test Table";
        begin
            case DocumentAttachment."Table ID" of
                DATABASE::"Test Table":
                    begin
                        RecRef.Open(DATABASE::"Test Table");
                        if TestTable.Get(DocumentAttachment."No.") then
                            RecRef.GetTable(TestTable);
                    end;
            end;
        end;
     
        [EventSubscriber(ObjectType::Table, Database::"Document Attachment",
                         'OnAfterInitFieldsFromRecRef', '', false, false)]
        local procedure DocumentAttachment_OnAfterInitFieldsFromRecRef(
            var DocumentAttachment: Record "Document Attachment";
            var RecRef: RecordRef)
        var
            FieldRef: FieldRef;
            RecNo: Code[20];
        begin
            case RecRef.Number of
                DATABASE::"Test Table":
                    begin
                        FieldRef := RecRef.Field(1);
                        RecNo := Format(FieldRef.Value);
                        DocumentAttachment.Validate("No.", RecNo);
                    end;
            end;
        end;
    }
     
    table 50100 "Test Table"
    {
        DataClassification = CustomerContent;
        LookupPageId = 50138;
     
        fields
        {
            field(1; ID; Code[20])
            {
                Caption = 'ID';
                DataClassification = CustomerContent;
            }
            field(2; Name; Text[100])
            {
                Caption = 'Name';
                DataClassification = CustomerContent;
            }
            field(3; Description; Text[250])
            {
                Caption = 'Description';
                DataClassification = CustomerContent;
            }
        }
     
        keys
        {
            key(PK; ID)
            {
                Clustered = true;
            }
        }
    }
     
    page 50139 "Test Card"
    {
        PageType = Card;
        SourceTable = "Test Table";
        ApplicationArea = All;
        Caption = 'test card';
     
        layout
        {
            area(content)
            {
                group(General)
                {
                    field(ID; Rec.ID) { ApplicationArea = All; }
                    field(Name; Rec.Name) { ApplicationArea = All; }
                    field(Description; Rec.Description) { ApplicationArea = All; }
                }
            }
     
            area(factboxes)
            {
                part("Attached Documents List"; "Doc. Attachment List Factbox")
                {
                    ApplicationArea = All;
                    Caption = 'Documents';
                    UpdatePropagation = Both;
                    SubPageLink = "Table ID" = const(Database::"Test Table"),
                                  "No." = field(ID);
                }
            }
        }
    }
     
    page 50138 "Test list"
    {
        PageType = List;
        SourceTable = "Test Table";
        ApplicationArea = All;
        Caption = 'test list';
        CardPageId = 50139;
     
        layout
        {
            area(Content)
            {
                repeater(content)
                {
                    field(ID; Rec.ID) { ApplicationArea = All; }
                    field(Name; Rec.Name) { ApplicationArea = All; }
                    field(Description; Rec.Description) { ApplicationArea = All; }
                }
            }
        }
    }

     
  • Suggested answer
    Chavda Dharmendra Profile Picture
    641 on at
    Document Attachment not updating when ID is changed
    In Standard business central this EventSubscriber use for Rename Attachment record.
     
    You can create a same EventSubscriber for Custom Table and try again. 
     
    codeunit 1173 "Document Attachment Mgmt"
     
    [EventSubscriber(ObjectType::Table, Database::"Job", 'OnAfterRenameEvent', '', false, false)]
        local procedure MoveAttachedDocumentsOnAfterRenameJob(var Rec: Record Job; var xRec: Record Job; RunTrigger: Boolean)
        var
            MoveFromRecRef: RecordRef;
            MoveToRecRef: RecordRef;
        begin
            // Moves attached docs when an Job record is renamed [When No. is changed] from old to new rec
            MoveFromRecRef.GetTable(xRec);
            MoveToRecRef.GetTable(Rec);
     
            MoveAttachmentsWithinSameRecordType(MoveFromRecRef, MoveToRecRef);
        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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sumit Singh Profile Picture

Sumit Singh 2,708

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 2,675 Super User 2025 Season 2

#3
Jeffrey Bulanadi Profile Picture

Jeffrey Bulanadi 2,203

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans