Single Instance codeunits are essentially an implementation of the Singleton Design Pattern. In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance.

Every object that refers to a codeunit unit of this type will receive the same instance.

I recently had the need to ask myself how can a global variable be accessed in business central through AL Extension Development?

I had found Single Instance Codeunit as the natural first choice. More research material online is needed around this type of codeunit, and I couldn't really get the needed answers to my questions. So I decided to do a quick experiment to figure it out.

To solidify my choice, I experimented with two separate Business Central Sessions as per below -

I created one Single Instance Codeunit, and One Counter Manager Page that Increments/Decrements a Counter variable in the Single Instance Codeunit. I launched two instances of the web client and noted my findings.

On the left, You will see the value of Counter as 3. We can exit the page, carry on with our business and when we come back to the page, we will still see the value as 3. Even if we keep incrementing the global counter in the second session.

I was able to deduce from this that Single Instance Codeunits aren't server side, but exist in the application scope, and the lifetime of the instance is limited to the current client. This was good enough for my purposes.

Find below the code -

codeunit 50100 "Counter Management" implements ICounterManagement
{
    SingleInstance = true;

    var
        counter: Integer;

    procedure Increment()
    begin
        counter += 1;
    end;

    procedure Decrement()
    begin
        counter -= 1;
    end;

    procedure GetCurrentCounter(): Integer
    begin
        exit(counter);
    end;
}

interface ICounterManagement
{
    procedure Increment();
    procedure Decrement();

    procedure GetCurrentCounter(): Integer;
}
codeunit 50100 "Counter Management" and interface ICounterManagement
page 50100 "Counter Manager"
{

    Caption = 'Counter Manager';
    PageType = Document;
    ApplicationArea = Suite;
    UsageCategory = Tasks;

    layout
    {
        area(content)
        {
            group(General)
            {
                field(Counter; CounterManagement.GetCurrentCounter())
                {
                    ApplicationArea = All;
                    Caption = 'Current Counter';
                }
            }
        }
    }

    actions
    {
        area(Processing)
        {
            action(IncrementCounter)
            {
                ApplicationArea = Basic, Suite;
                Caption = 'Increment Global Counter';
                Image = Add;
                trigger OnAction()
                begin
                    CounterManagement.Increment();
                    CurrPage.Update();
                end;
            }
            action(DecrementCounter)
            {
                ApplicationArea = Basic, Suite;
                Caption = 'Decrement Global Counter';
                Image = Delete;
                trigger OnAction()
                begin
                    CounterManagement.Decrement();
                    CurrPage.Update();
                end;
            }
        }
    }
    var
        CounterManagement: Codeunit "Counter Management";
}
page 50100 "Counter Manager"

Code here:

Kushal5/Single-Instance-Codeunits-BC
How can a global variable be accessed in business central through AL Extension Development? - Kushal5/Single-Instance-Codeunits-BC

More details here:

https://docs.microsoft.com/en-us/dynamics-nav/singleinstance-property

http://partnersource.ru/cside.en/html/0456b0e3-873e-427b-8ef5-b9bb0ac7ba6e.htm