Giuseppe Maggiore
Giuseppe Maggiore

Reputation: 2031

Automatically setting parent id when creating item from list part in Business Central/AL

I am quite new to building extensions with AL for Business Central. I am trying to setup an extension for a school application. The tables I have built work, and they follow this data model:

School - Course - Lecture
  |     /
Teacher

Inside the Card for School, I am showing a list part for Course. It correctly shows all courses of a given school. So far so good. But now, whenever I create a Course from this view, I have to remember to set the SchoolId manually, but I would like to do this automatically because we already know which School we are in.

The Course table looks like this:

table 50110 Course
{
    DataClassification = ToBeClassified;
    DrillDownPageId = "Course List";
    LookupPageId = "Course List";


    fields
    {
        field(1; "No."; Code[20])
        ...
        field(5; "SchoolId"; Code[20])
        {
            DataClassification = ToBeClassified;
            TableRelation = School."No.";
        }
    }

    keys
    {
        key(PK; "No.")
        {
            Clustered = true;
        }
    }
}

The Course list part explicitly does not contain the SchoolId, as we would expect this to be managed automatically:

page 50118 "Course List Part"
{
    PageType = ListPart;
    UsageCategory = None;
    SourceTable = Course;
    CardPageId = "Course Card";
    // InsertAllowed = false;

    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field("No."; "No.") { ApplicationArea = All; }
                field(Name; Name) { ApplicationArea = All; }
                field(CourseOwnerId; CourseOwnerId) { ApplicationArea = All; }
                // field(SchoolId; SchoolId) { ApplicationArea = All; }
            }
        }
    }
}

The School card invokes the Course list part on the appropriate view:

page 50117 "School Card"
{
    PageType = Card;
    UsageCategory = None;
    SourceTable = School;

    layout
    {
        area(Content)
        {
            group(General)
            {
                field("No."; "No.")
                {
                    ApplicationArea = All;

                }
                field(Name; Name)
                {
                    ApplicationArea = All;

                }
            }
            group("Extras 1")
            {
                part("Courses"; "Course List Part")
                {
                    ApplicationArea = All;
                    SubPageLink = SchoolId = field("No.");
                    UpdatePropagation = Both;
                }
            }
        }
    }
}

And of course, the School table, where the No. property is set as the primary key:

table 50113 School
{
    DataClassification = ToBeClassified;
    DrillDownPageId = "School List";
    LookupPageId = "School List";

    fields
    {
        field(1; "No."; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        ...
    }

    keys
    {
        key(PK; "No.")
        {
            Clustered = true;
        }
    }
}

Still, no luck.

Upvotes: 0

Views: 947

Answers (1)

JeffUK
JeffUK

Reputation: 4251

When you add the 'Course' page as to the 'School page', the sub-page link will automatically handle that part of the relationship for you; when you insert a new record it will automatically populate No. with 'SchoolId'.

e.g. this is how it works on the Sales Order page.

 part(SalesLines; "Sales Order Subform")
            {
                ApplicationArea = Basic, Suite;
                Editable = DynamicEditable;
                Enabled = "Sell-to Customer No." <> '';
                SubPageLink = "Document No." = FIELD("No.");
                UpdatePropagation = Both;
            }

You must also define a table relation from the 'Child' table to the 'Parent' table, e.g.

table 50121 child
{ 
    fields
    {
        field(1; ParentID; Integer)
        {          
            TableRelation = Parent.ID;

The field you're linking on must be a primary key of the Child table, i.e. 'SchoolID' must be in the primary key of 'course' in your code.

Upvotes: 1

Related Questions