Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

Problem with AL Programming for a university project

(0) ShareShare
ReportReport
Posted on by

Problem Description

I am working on creating a table and a page in Business Central using AL. Below is the table and page code I’ve implemented:

Table Code:

table 123456700 "DIM_Mitarbeiter"
{
Caption = 'DIM_Mitarbeiter';
DataClassification = ToBeClassified;

fields
{
field(1; "No."; Code[20])
{
Caption = 'Mitarbeiter Nr.';
DataClassification = ToBeClassified;
}
field(2; "Department"; Text[30])
{
Caption = 'Abteilung';
DataClassification = ToBeClassified;
}
}

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

Page Code:

page 123456700 "DIM_Mitarbeiter_Page"
{
ApplicationArea = All;
Caption = 'DIM_Mitarbeiter_Page';
PageType = Card;
SourceTable = DIM_Mitarbeiter;

layout
{
area(content)
{
repeater(Group)
{
field("No."; "No.")
{
ApplicationArea = All;
Caption = 'Mitarbeiter Nr.';
}
field("Department"; "Department")
{
ApplicationArea = All;
Caption = 'Abteilung';
}
}
}
}

actions
{
area(Processing)
{
action(InsertNew)
{
Caption = 'Neuen Mitarbeiter hinzufügen';
ApplicationArea = All;
trigger OnAction()
begin
CurrPage.InsertRecord();
end;
}
}
}
}

Errors Encountered

  1. Error: The name 'No.' does not exist in the current context.

    • This error occurs for the field("No."; "No.") line in the page code.
    • However, the No. field is properly defined in the table as field(1; "No."; Code[20]).
  2. Error: The name 'Department' does not exist in the current context.

    • This error occurs for the field("Department"; "Department") line in the page code.
    • The Department field is also defined in the table as field(2; "Department"; Text[30]).
  3. Error: 'CurrPage.InsertRecord()' does not exist.

    • This occurs in the InsertNew action of the page.
    • I’ve set the PageType to Card, but the InsertRecord method does not seem to work with Card.

Steps I’ve Tried

  1. Verified the Table Definition:

    • Both No. and Department fields are defined in the table.
    • The table has been saved and published successfully.
  2. Changed the PageType:

    • I attempted to change the PageType from Card to List, but the issue persisted.
  3. Recompiled and Republished:

    • The project was recompiled using AL: Publish Without Debugging.
    • No compilation errors were reported.

Expected Behavior

  1. The page should correctly display the No. and Department fields from the table.
  2. The InsertRecord action should allow adding new records to the table.

Questions

  1. Why is the page not recognizing the fields (No., Department) defined in the table?
  2. How can I resolve the 'InsertRecord' does not exist error for a Card or List page?

Any help or insights would be greatly appreciated!

  • Suggested answer
    Alexander Drogin Profile Picture
    Alexander Drogin 123 on at
    Problem with AL Programming for a university project
    You can't see the records because the page is not refreshed after the insert. If you want to update records from a page action and see the result in the same page, you need to call CurrPage.Update(false) after inserting records.
    And on a separate note, although not related to the problem. It is not considered a good practice to keep application logic in UI actions. A much better way is to move the code into a procedure declared in a codeunit, and call this procedure from the action trigger. If you declare "No." and Department as parameters of this procedure, you can eliminate code duplication and call the procedure four times with different sets of arguments.
  • Verified answer
    CU15012354-0 Profile Picture
    CU15012354-0 on at
    Problem with AL Programming for a university project
    thank you to everyone who helped me. 
    I still had a question. I have now further developed the code and now have the problem that the page 
    that i created is not filled with data that is relevant to the task. 
    page 123456700 "DIM_Mitarbeiter_Page"
    {
        ApplicationArea = All;
        Caption = 'DIM_Mitarbeiter_Page';
        PageType = List;
        SourceTable = DIM_Mitarbeiter;
     
        layout
        {
            area(content)
            {
                repeater(Group)
                {
                    field("No."; Rec."No.")
                    {
                        ApplicationArea = All;
                        Caption = 'Mitarbeiter Nr.';
                    }
                    field("Department"; Rec."Department")
                    {
                        ApplicationArea = All;
                        Caption = 'Abteilung';
                    }
                }
            }
        }
     
        actions
        {
            area(Processing)
            {
                action(FillDefaultData)
                {
                    Caption = 'Standarddaten einfügen';
                    ApplicationArea = All;
                    trigger OnAction()
                    var
                        RecDimMitarbeiter: Record "DIM_Mitarbeiter";
                        Department: Text[30];
                    begin
                        // Alle bestehenden Einträge löschen
                        RecDimMitarbeiter.DeleteAll();
     
                        // Standard-Datensätze einfügen
                        Department := 'Produktion';
                        RecDimMitarbeiter.Init();
                        RecDimMitarbeiter."No." := 'M001';
                        RecDimMitarbeiter."Department" := Department;
                        RecDimMitarbeiter.Insert();
     
                        Department := 'Verwaltung';
                        RecDimMitarbeiter.Init();
                        RecDimMitarbeiter."No." := 'M002';
                        RecDimMitarbeiter."Department" := Department;
                        RecDimMitarbeiter.Insert();
     
                        Department := 'Verkauf';
                        RecDimMitarbeiter.Init();
                        RecDimMitarbeiter."No." := 'M003';
                        RecDimMitarbeiter."Department" := Department;
                        RecDimMitarbeiter.Insert();
     
                        Department := 'Produktion';
                        RecDimMitarbeiter.Init();
                        RecDimMitarbeiter."No." := 'M004';
                        RecDimMitarbeiter."Department" := Department;
                        RecDimMitarbeiter.Insert();
     
                        Message('Standarddaten wurden erfolgreich eingefügt.');
                    end;
                }
            }
        }
    }
     
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 5,954 Moderator on at
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 5,954 Moderator on at
    Problem with AL Programming for a university project
    Agree with the previous response for points 1 and 2; try using the Rec.FieldName.
    As for point 3, CurrPage.InsertRecord() is not a valid function. What exactly are you trying to achieve with that?
  • Alexander Drogin Profile Picture
    Alexander Drogin 123 on at
    Problem with AL Programming for a university project
    To add to the previous answer: what you are trying to do accessing tables fields in the page is called "implicit with statement". It used to work in older versions of BC and NAV, but is being deprecated. So now you need to give the explicit record variable in the page.
  • Suggested answer
    Ramiz Profile Picture
    Ramiz 455 on at
    Problem with AL Programming for a university project
    Hi,

    You need to use Rec with fields
     
    field("No."; Rec."No.")
    {
    	ApplicationArea = All;
    	Caption = 'Mitarbeiter Nr.';
    }
    field("Department"; Rec."Department")
    {
    	ApplicationArea = All;
    	Caption = 'Abteilung';
    }
    There is no function called CurrPage,InsertRecord. For insertion you need to create a logic. You can check existing logic like customer and vendor and use that. Also for automatic numbering you should use no series.

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,684 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans