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 :
Business Central forum

Custom form to manage One to Many relation

(0) ShareShare
ReportReport
Posted on by 10

Dear All,

I'm new to developing in Dynamic 365 Business Central, i have build a new page of type Document to manage data entry for 2 tables having one to many relation, something similar to Sales Order document.

I added part control in the form in order to display details from the child table in a List part page, the sub page displays rows filtered based on parent table primary key.

the problem is while adding new line in the sub page, i must manually select foreign  ID from the lookup otherwise error occurred, how to make  this field automatically populated based on the primary key of parent table.

regards,

Categories:
I have the same question (0)
  • Krzysztof Bialowas Profile Picture
    500 on at

    Hi,

    I looked briefly thru your code and first thing is that you have wrong keys in your tables. Please check how it is done for example for sales header and sales line. In the line table you have to have primary key which contains the key from header. In this case I would also not use auto incremental field but would do it in the Ledger Entries way (looking for last one and add 1 to the result).

    Those are first things which I notice.

    Best regards,

    Krzysztof

  • Arifai Profile Picture
    10 on at

    Thanks Krzysztof for your reply,

    I have added AutoSplitKey property to subpage but nothing changed below you can find the code I’m using.

    I have highlighted, code where I added subpage and where I inserted AutoSplitKey.

     

    Note: as I mentioned before, subpage displays fine but after adding new line I get this error.

    The question could be, how to pass parameters from main page to sub page or other page and use this parameter inn the subpage or the newly opened one.

    Thank you in advance

    Parent Table

    table 50110 EmpLeaveRequest

    {

        DataClassification = ToBeClassified;

     

        fields

        {

            field(10; EmpLeaveRequestId; Integer)

            {

                DataClassification = ToBeClassified;

                AutoIncrement = true;

     

            }

     

            field(20; EmployeeId; code[20])

            {

                TableRelation = Employee."No.";

                ValidateTableRelation = true;

                NotBlank = true;

     

                trigger OnValidate();

                begin

                    CALCFieldS("EmployeeName");

                end;

            }

            field(22; EmployeeName; Text[500])

            {

                Caption = 'Employee Name';

                CalcFormula = Lookup (Employee."Search Name" where ("No." = Field ("EmployeeId")));

                Editable = false;

                FieldClass = FlowField;

            }

           

            field(40; RequestDate; Date)

            {

                NotBlank = true;

            }

     

            field(90; FromDate; Date)

            {

                NotBlank = true;

            }

            field(100; ToDate; Date)

            {

                NotBlank = true;

            }

           

        }

     

        keys

        {

            key(PK; EmpLeaveRequestId)

            {

                Clustered = true;

            }

     

        }

    }

     

     

    Child Table

    table 50113 EmpLeaveReqTypeCrdAndAvailed

    {

        DataClassification = ToBeClassified;

     

        fields

        {

            field(10; EmpLeaveReqTypeCrdAndAvailedId; Integer)

            {

                DataClassification = ToBeClassified;

                AutoIncrement = true;

     

            }

            field(20; EmpLeaveRequestId; Integer)

            {

                TableRelation = EmpLeaveRequest.EmpLeaveRequestId;

                ValidateTableRelation = true;

                NotBlank = true;

            }

           

            field(40; Credits; Integer)

            {

               

     

            }

            field(50; Availed; Integer)

            {

               

            }

        }

     

        keys

        {

            key(PK; EmpLeaveReqTypeCrdAndAvailedId)

            {

                Clustered = true;

            }

        }

    }

     

    Subpage code

    page 50125 EmpLRTypeCrdAndAvailedSubList

    {

        PageType = ListPart;

        ApplicationArea = All;

        UsageCategory = Lists;

        SourceTable = EmpLeaveReqTypeCrdAndAvailed;

        Caption = 'Employee Credit and Availed Dates';

        AutoSplitKey = true;

     

        layout

        {

            area(Content)

            {

                repeater(GroupName)

                {

     

                    field("EmpLeaveRequestId"; EmpLeaveRequestId)

                    {

                        ApplicationArea = All;

                        Caption = 'Emp Leave Request Id';

                    }

     

                    field("Leave Type"; LeaveTypeId)

                    {

                        ApplicationArea = All;

                        Lookup = true;

                        LookupPageId = LeaveTypeList;

                        Caption = 'Leave Type';

     

                    }

                    field("LeaveTypeName"; LeaveTypeName)

                    {

                        ApplicationArea = All;

                        Caption = 'Leave Type Name';

                    }

                    field("Credits"; Credits)

                    {

                        ApplicationArea = All;

                    }

                    field("Availed"; Availed)

                    {

                        ApplicationArea = All;

                    }

                   

                }

     

            }

     

        }

    }

     

    Main / parent Page code

    page 50121 LeaveRequestCard

    {

        PageType = Document;

        ApplicationArea = All;

        UsageCategory = Administration;

        SourceTable = EmpLeaveRequest;

        Caption = 'Employee Leave Request';

        DataCaptionExpression = "EmployeeName";

        layout

        {

            area(Content)

            {

                group("Employee Details")

                {

     

                    field("EmpLeaveRequestId"; EmpLeaveRequestId)

                    {

                        ApplicationArea = All;

                        Editable = false;

                    }

     

                    field("Employee"; EmployeeId)

                    {

                        ApplicationArea = All;

                        Caption = 'Employee';

                        Lookup = true;

     

                    }

                    field("EmployeeName"; EmployeeName)

                    {

                        ApplicationArea = All;

                        Caption = 'Employee Name';

                    }

                }

     

                group("LeaveRequestDetails")

                {

                    Caption = 'Leave Request Details';

                    field("DateFiled"; RequestDate)

                    {

                        ApplicationArea = All;

                        Caption = 'Date Filed';

                    }

                   

                    field("FromDate"; FromDate)

                    {

                        ApplicationArea = All;

                        Caption = 'From Date';

                    }

                    field("ToDate"; ToDate)

                    {

                        ApplicationArea = All;

                        Caption = 'To Date';

                    }

                }

     

     

                group("HR Department")

                {

                   

    part("Part1"; "EmpLRTypeCrdAndAvailedSubList")

                                {

                                    ApplicationArea = All;

                                    SubPageLink = "EmpLeaveRequestId" = field (EmpLeaveRequestId);

     

                                }

     

                }

     

               

            }

     

        }

    }

     

  • Krzysztof Bialowas Profile Picture
    500 on at

    Hi,

    If you have the Document Page make sure that you open it with proper SubPageLink and also since you have Line No. in lines table that AutoSplitKey option need to be true on SubPage.

    If this not work for you please share somehow the code will try to help you.

    Best regards,

    Krzysztof

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans