Skip to main content

Protected Variable in AL

We can 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 me give an example to get a better understanding. I have a core extension with a new page called “My Item List”.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.

1
2
3
4
5
6
7
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.

Source: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-protected-variables

Comments

Popular posts from this blog

How to add editable field in Ledger Entries in Business Central?

  How to add editable field in Ledger Entries in Business Central? Many times we need to make an editable field in the business central on Customer Ledger entries or General Ledger Entries or Item Ledger Entries. On the Ledger  Entries, we can not add directly editable fields on the page because these Ledger Entry pages are designed to not allow modify fields because of below code. So, I want to add field called comment on the General Ledger Entries. Now, what we can do if we want to add editable field on the General Ledger Entries? Firstly, we will extend table and add comment field and then will extend general ledger entries and add our field at the last. tableextension   50261  tableextension50261  extends  "G/L Entry" {      fields     {          // Add changes to table fields here          field( 50261 ; Comments;  Text [ 250 ] )         {              DataClassification  = CustomerContent;             Caption =  'Comments' ;         }     } } pageextension  

Custom API in Business Central

Today, I will demonstrate how to work with custom API in Business Central. Custom API is very useful when we are working with integration apps. So, lets start exploring custom API in business central. We will take one table called student and will create one API page in order to access this table as API. Moreover, we can check this API in Postman as well. Here, we will mainly perform three operations GET, Post, and Delete. First of all, we will create student Table with four fields ID, First Name, Last Name and Email. For the primary key we will take ID. Once table is added, we will create one list page to view our data. So, we can easily check our custom API is working properly or not. Now, we will add another page which is used as API. This is a new page type introduced in business central. You can check how to create API page in business central.( Click Here ) Now, we can interact with student  data with the help of this API page in the business central. We will write a logic i

Import Excel File in Business Central

Import Excel in Business Central Many times we need to import data through excel file in the business central / navision. We used to perform this kind of activity in the navision but we need to explore the thing in business central. So, i am trying to import data from excel file in the business central. I am using business central wave 1 2020 for the development. We will create a table called student and add some fields to test imported data from Excel in Business Central. Now, We will add a new list page to view imported data from excel. Now, We will create a new report to import data from the excel in business central. If you worked with the navision then you might be noticed that some of the functions are not working in the business central due to scope issue. So, we will replace those functions which we used in NAV and try some new available functions to import excel. Now, we will add an action to the list page for import data