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...
New Discussion

MODAL errors during transactions: how to catch the fox

(1) ShareShare
ReportReport
Posted on by Microsoft Employee

 

A typical error is thrown when the application is trying to issue some not allowed functions executed in their MODAL equivalent.

 pastedimage1608299791339v1.png

This error, then, is not a platform issue but a typical application one (sub-optimal code).

Typically, this is hard to catch at runtime since there might be nested code and, overall, conditional statement that may or may not lead to hit the modal functions.

 

How do you catch the fox, then? There might be 2 feasible way:

 

  1. Automated Tests.

The more tests you create – and execute them – the lower is the risk to smash your face in production at runtime.

That is the warm suggested solution.

 

  1. Use a dummy modal call in a try function before the real modal call.

Let me explain the trick. It might seem complex but actually it is not.

Create a codeunit whom is performing a call to itself under a try function.

 

codeunit 50112 "Write Transaction Checker"

{

    trigger OnRun()

    begin        

    end;

    

    procedure Check() : Boolean;

    begin

        exit(not(CheckInternal));

    end;  

 

    [TryFunction]

    local procedure CheckInternal()

    begin

        if Codeunit.Run(50112) then;

    end;

 

}

To simulate how the checker is working just create and execute a codeunit like the following

 

codeunit 50113 "Test Transaction Type"

{

    trigger OnRun()

    var

        writeTransactionChecker : Codeunit "Write Transaction Checker";

        item : Record Item;

        isInWriteTransaction : Boolean;

    begin

        

        isInWriteTransaction := writeTransactionChecker.Check;

        Message('Is in write transaction: %1',isInWriteTransaction);

 

        item.LockTable();

        item."No." := 'TESTITEM';

        item.Insert();

 

        isInWriteTransaction := writeTransactionChecker.Check;

        Message('Is in write transaction: %1',isInWriteTransaction);

 

    end;

    

}

 

The first message would not have a transaction started hence it will report a “No”.

At this stage, performing a modal action would be safe.

 

The second message is the result of a codeunit.run with a return value under a write transaction hence it will report a “Yes”.

You might want to use the Boolean value isInWriteTransaction to perform a different business process decision that do not involve a modal execution.

 

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 624 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 440 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 244 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans