Skip to content
Arthur edited this page Nov 20, 2023 · 4 revisions

Clear(All) does not affect or change values for global variables in single instance codeunits.

Using Clear() on a single instance codeunit variable does not affect the global variables for these codeunits and could lead to unexpected results. This also applies to ClearAll() in a object (page, codeunit, ..) where local or global single instance codeunit variables are defined.

Example on Clear()

codeunit 50100 MyCodeunit
{
    procedure DoSomething()
    var
        MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
    begin
        Clear(MySingleInstanceCodeunit); // This will not(!) clear the GlobalDocumentNumber variable in the MySingleInstanceCodeunit
    end;
}

codeunit 50101 MySingleInstanceCodeunit
{
    SingleInstance = true;

    var
        GlobalDocumentNumber: Code[20];
}

Example on ClearAll()

codeunit 50100 MyCodeunit
{
    var
        MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;

    procedure DoSomething()
    begin
        ClearAll(); // This will not(!) clear the GlobalDocumentNumber variable in the MySingleInstanceCodeunit
    end;
}

codeunit 50101 MySingleInstanceCodeunit
{
    SingleInstance = true;

    var
        GlobalDocumentNumber: Code[20];
}

Exceptions

Both functions, Clear() and ClearAll(), do not clear variables of the type Record. So this rule will ignore these types of variable.

codeunit 50100 MyCodeunit
{
    var
        MySingleInstanceCodeunitWithOnlyGlobalRecords: Codeunit MySingleInstanceCodeunitWithOnlyGlobalRecords;

    procedure DoSomething()
    begin
        // Do not raise a LC0032 rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
        Clear(MySingleInstanceCodeunitWithOnlyGlobalRecords);

        // Do not raise a LC0032 rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
        ClearAll();
    end;
}

codeunit 50101 MySingleInstanceCodeunitWithOnlyGlobalRecords
{
    SingleInstance = true;

    var
        GlobalCustomerRec: Record Customer;
}

There's also an exception though for when the single instance codeunit, that is found, is the same as the codeunit itself.

codeunit 50100 "My Single Instance Codeunit"
{
    EventSubscriberInstance = Manual;
    SingleInstance = true;

    var
        MySingleInstanceCodeunit: Codeunit "My Single Instance Codeunit";
        myBoolean: Boolean;
        myDecimal: Decimal;

    #region ClearEverythingThatIsStored
    internal procedure ClearEverythingThatIsStored()
    begin
        // Do not raise a LC0032 rule here (MySingleInstanceCodeunit is the same object as itself)
        ClearAll();
    end;
    #endregion ClearEverythingThatIsStored

    #region DoSomething
    internal procedure DoSomething(): Decimal;
    begin
        // Just some code
        if BindSubscription(MySingleInstanceCodeunit) then
            if myBoolean then
                exit(myDecimal);
    end;
    #endregion DoSomething

// EventSubscriber that is only active when this codeunit is Bound.

External references

Clone this wiki locally