Protected Variables in AL

Protected Variables

If you want to make a global variable accessible from table to table extension, or from page to page extension, you would normally need to create a procedure to get or set the variables. With protected variables, you no longer need to do that.

The protected keyword can be used to make variables accessible between tables and table extensions and between pages and page extensions. If you want to only expose some variables as protected, you must create two sections of variables declarations.

Let’s give an example. I have a core extension with a new page called “My Item List”.

page 70102 "My Item List"
{

    Caption = 'My Item List';
    PageType = List;
    SourceTable = Item;

    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("No."; Rec."No.")
                {
                    ApplicationArea = All;
                }
                field(Description; Rec.Description)
                {
                    ApplicationArea = All;
                }
            }
        }
    }

    protected var
        AccessibleVar: Integer; //accessible to page extension

    var
        NotAccessibleVar: Integer; //not accessible to page extension
}

I have another extension that depends on the core extension. If I do a page extension to My Item List page, I can access the protected variable on the original page.

pageextension 70122 "My Item List Ext" extends "My Item List"
{
    trigger OnOpenPage()
    begin
        AccessibleVar := 1;
    end;
}

Protected variables is only available on Business Central 2019 release wave 2 and later.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *