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

Notifications

Announcements

Community site session details

Community site session details

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

attachments in factbox using 2 primary keys

(1) ShareShare
ReportReport
Posted on by 48
 Hi there, 
After attaching a file, the DocumentAttachment."No." and DocumentAttachment.EmpCode fields are not being updated. How can I ensure these fields are correctly updated? Below is the code and events I have subscribed to, and I have referred to YUN ZHU sir's blog. 

 
codeunit 50204 DocumentAttachment
{
    [EventSubscriber(ObjectType::Page, Page::"Document Attachment Factbox", 'OnBeforeDrillDown', '', false, false)]
    local procedure OnBeforeDrillDown(DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef);
    var
        Employee: Record Employees;
    begin
        case DocumentAttachment."Table ID" of
            DATABASE::Employees:
                begin
                    RecRef.Open(DATABASE::Employees);
                    Employee.SetRange(EmpID, DocumentAttachment."No.");
                    Employee.SetRange(EmpCode, DocumentAttachment.EmpCode);
                    if Employee.FindFirst() then begin
                        RecRef.GetTable(Employee);
                    end;
                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];
        EmpCd: Code[10];
        EmpTyp: Enum EmployeeType;
    begin
        case RecRef.Number of
            DATABASE::Employees:
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.SetRange("No.", RecNo);
 
                    FieldRef := RecRef.Field(4);
                    EmpCd := FieldRef.Value;
                    DocumentAttachment.SetRange(EmpCode, EmpCd);
                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];
        EmpCd: Code[10];
        EmpTyp: Enum EmployeeType;
    begin
        case RecRef.Number of
            DATABASE::Employees:
                begin
                    FieldRef := RecRef.Field(1);
                    RecNo := FieldRef.Value;
                    DocumentAttachment.Validate("No.", RecNo);
 
                    FieldRef := RecRef.Field(4);
                    EmpCd := FieldRef.Value;
                    DocumentAttachment.Validate(EmpCode, EmpCd);
                end;
        end;
    end;
}
I have the same question (0)
  • Suggested answer
    Gerardo Rentería García Profile Picture
    25,398 Most Valuable Professional on at
    Hi
    Can you review this event? Apparently a parameter is missing.
     
       [IntegrationEvent(false, false)]
        local procedure OnAfterOpenForRecRef(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef; var FlowFieldsEditable: Boolean)
        begin
        end;
    BR
    GR
  • Syed Safwan Profile Picture
    48 on at
    Thank you gdrenteria, for your swift reply. I tried adding the parameter and tested it but still fields data is not being assigned.
  • Suggested answer
    Yi Yong Profile Picture
    2,602 Super User 2025 Season 2 on at
    Hello Safwan,
     
    I see that you are using custom Employees table.
    Is the EmpCode's field ID = 4 on this custom table?
     
    Personally, I haven't tried using the two keys method.
     
    Does it work if you are using EmpID?
  • CU19110831-0 Profile Picture
    2 on at
    I have exactly the same issue, I am trying to use the Document Attachement with a custom table that has 2 fields as primary key but it doesn't work as expected.
    The fields DocumentAttachment."No." and DocumentAttachment.CustomField would not get a value after the document attachement is uploaded, when I use the Attachement factbox link (by clicking the number of items). 
    With the exact same code for the event subscribers, everything works great if I use an action from my page using the following trigger:
     
                    trigger OnAction()
                    var
                        DocumentAttachmentDetails: Page "Document Attachment Details";
                        RecRef: RecordRef;
                    begin
                        RecRef.GetTable(Rec);
                        DocumentAttachmentDetails.OpenForRecRef(RecRef);
                        DocumentAttachmentDetails.RunModal();
                    end;
     
    I think that the problem is at OnBeforeDrillDown where the selected row from my custom table is not passing through to the factbox and therefore the RecRef  although has the correct table structure doesn't have the data.

    Does anyone has a suggestion where to check/change something?

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,573

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 773 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 760 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans