Skip to main content

Notifications

Announcements

No record found.

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 36
 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;
}
  • CU19110831-0 Profile Picture
    2 on at
    attachments in factbox using 2 primary keys
    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?
  • Suggested answer
    Yi Yong Profile Picture
    1,907 Super User 2025 Season 1 on at
    attachments in factbox using 2 primary keys
    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?
  • Syed Safwan Profile Picture
    36 on at
    attachments in factbox using 2 primary keys
    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
    gdrenteria Profile Picture
    15,506 Most Valuable Professional on at
    attachments in factbox using 2 primary keys
    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,978 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,821 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans