0

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.

Giuseppe Maggiore
  • 2,011
  • 1
  • 23
  • 31

1 Answers1

1

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.

JeffUK
  • 4,107
  • 2
  • 20
  • 34
  • 1
    Thanks, I just tried, but unfortunately I did not manage to get your code to working. The SubPageLink alone does not take care of the update, and `No.` is definitely in the primary key of `Course` in my example. I have neither `Enabled`, nor `Editable`, but I cannot imagine why they would need to be there to implement this behaviour (and I don't know why (still beginner), but `DynamicEditable` does not compile). Any idea what else I could try? – Giuseppe Maggiore May 17 '20 at 17:50
  • Just tried it out myself, and realised we both missed something else, evidently a tablerelation needs to be defined from the child table's field too, makes sense I guess, had you done that? Updated answer. – JeffUK May 17 '20 at 19:26
  • Curiouser and curiouser, because indeed I would expect this to work like you describe. I am updating my original question with more code. It looks aligned to what you suggest as far as my inexperienced eyes can see... – Giuseppe Maggiore May 18 '20 at 12:35
  • I tried adding the `TableRelation = Parent.Id`, because it was `TableRelation = Parent`, but still: no luck. – Giuseppe Maggiore May 18 '20 at 12:42
  • Ahha, I think schoolid must be part of the primary key of the courses table. – JeffUK May 18 '20 at 18:03
  • YES! It works!!! Weird and a bit unexpected/obscure, but certainly not absurd. Interesting to note, is that after adding the SchoolId to the PK of the Course table, the Course list part is still editable, but to add an item I now have to click on Actions -> New, instead of just getting an extra empty line where I can start typing. When Choosing Actions -> New, then I get the SchoolId prefilled. Any idea what this means, or even where this behaviour would be documented? – Giuseppe Maggiore May 19 '20 at 08:24