web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Test Codeunit doesn't recognize PageHandler

(0) ShareShare
ReportReport
Posted on by 84

I've written a test codeunit that is supposed to invoke an action on a page that opens a window, so I've defined a PageHandler in the HandlerFunctions property. However, the tests still throw an "Unhandled UI" error.

Here is the full Test Codeunit:

codeunit 78000 "Changelog Action Test CCL"
{
    Description = 'Change Log Plus Action';
    Subtype = Test;

    trigger OnRun();
    begin
        IsInitialized := false;

        OpenChangeLogPlus_ItemList();
        OpenChangeLogPlus_ItemCard();
    end;

    var
        LibraryRandom: Codeunit "Library - Random";
        LibrarySetupStorage: Codeunit "Library - Setup Storage";
        LibraryTestInitialize: Codeunit "Library - Test Initialize";
        LibraryVariableStorage: Codeunit "Library - Variable Storage";
        Assert: Codeunit "Library Assert";
        IsInitialized: Boolean;
        sEntryFilter: Text;

    [Test]
    [HandlerFunctions('ChangeLogPlusPageHandler')]
    procedure OpenChangeLogPlus_ItemList()
    var
        recItem: Record Item;
        pagItemList: TestPage "Item List";
    begin
        // [SCENARIO #0001]
        // [GIVEN] A randomly selected item on the Item List page
        Initialize();

        GetRandomItem(recItem);
        pagItemList.OpenView();
        pagItemList.GoToRecord(recItem);

        // [WHEN] Invoking the "Change Log Plus" action
        pagItemList."Change Log Plus CCL".Invoke();
        pagItemList.Close();

        // [THEN] Filter matches Item Table No. only
        Assert.AreEqual(recItem.GetFilters, sEntryFilter, 'Filter doesn''t match');
    end;

    [Test]
    [HandlerFunctions('ChangeLogPlusPageHandler')]
    procedure OpenChangeLogPlus_ItemCard()
    var
        recItem: Record Item;
        pagItemCard: TestPage "Item Card";
    begin
        // [SCENARIO #0001]
        // [GIVEN] A randomly selected item on the Item Card page
        Initialize();

        GetRandomItem(recItem);
        pagItemCard.OpenView();
        pagItemCard.GoToRecord(recItem);

        // [WHEN] Invoking the "Change Log Plus" action
        pagItemCard."Change Log Plus CCL".Invoke();
        pagItemCard.Close();

        // [THEN] Filter matches Item Table No. & Primary Key 1 only
        Assert.AreEqual(recItem.GetFilters, sEntryFilter, 'Filter doesn''t match');
    end;

    [PageHandler]
    procedure ChangeLogPlusPageHandler(var ChangeLogPlus: Page "Change Log Plus CCL");
    var
        recChangeLogEntry: Record "Change Log Entry";
    begin
        Clear(recChangeLogEntry);
        ChangeLogPlus.GetRecord(recChangeLogEntry);
        sEntryFilter := recChangeLogEntry.GetFilters();
        ChangeLogPlus.Close();
    end;

    local procedure CreateItems(iCount: Integer)
    var
        recItem: Record Item;
        iIndex: Integer;
    begin
        if iCount <= 0 then
            iCount := 1;

        while iIndex < iCount do begin
            Clear(recItem);

            recItem."No." := LibraryRandom.RandText(8);
            if recItem.Insert(true) then
                iIndex  = 1;
        end;
    end;

    local procedure GetRandomItem(var recItem: Record Item)
    var
        iSkip: Integer;
    begin
        recItem.FindFirst();
        iSkip := LibraryRandom.RandIntInRange(0, recItem.Count - 1);
        recItem.Next(iSkip);
    end;

    local procedure Initialize();
    begin
        LibraryTestInitialize.OnTestInitialize(Codeunit::"Changelog Action Test CCL");
        ClearLastError();
        LibraryVariableStorage.Clear();
        LibrarySetupStorage.Restore();
        if IsInitialized then
            exit;

        LibraryTestInitialize.OnBeforeTestSuiteInitialize(Codeunit::"Changelog Action Test CCL");

        LibraryRandom.Init();

        // CUSTOMIZATION: Prepare setup tables etc. that are used for all test functions
        CreateItems(5);

        IsInitialized := true;
        Commit();

        // CUSTOMIZATION: Add all setup tables that are changed by tests to the SetupStorage, so they can be restored for each test function that calls Initialize.
        // This is done InMemory, so it could be run after the COMMIT above
        //   LibrarySetupStorage.Save(DATABASE::"[SETUP TABLE ID]");

        LibraryTestInitialize.OnAfterTestSuiteInitialize(Codeunit::"Changelog Action Test CCL");
    end;
}

And here is the procedure that is executed by the invoked action:

procedure OpenChangeLogFromRec(recordVariant: Variant; filterKeyBoolean: Boolean)
var
    recChangeLogEntry: Record "Change Log Entry";
    cuDataTypeManagement: Codeunit "Data Type Management";
    inputRecordRef: RecordRef;
    invalidErr: Label 'Not a valid Record: %1', Comment = '%1: The Variant passed to the procedure.';
begin
    if not cuDataTypeManagement.GetRecordRef(recordVariant, inputRecordRef) then
        Error(invalidErr, Format(recordVariant));

    Clear(recChangeLogEntry);
    recChangeLogEntry.SetRange("Table No.", inputRecordRef.Number);
    if filterKeyBoolean then
        recChangeLogEntry.SetRange("Primary Key Field 1 Value", inputRecordRef.KeyIndex(1).FieldIndex(1).Value);
    Page.Run(Page::"Change Log Plus CCL", recChangeLogEntry);
end;

Lastly, here's the error message, as well as the page ID.

pastedimage1679407181089v1.png

pastedimage1679407254240v2.png

  • Suggested answer
    Marco Mels Profile Picture
    Microsoft Employee on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist. If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    www.dynamicsuser.net/.../14

    I will open this up to the community in case they have something to add.

    Thanks.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 545 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 354 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 228 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans