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

Global variables in a single instance codeunit

(0) ShareShare
ReportReport
Posted on by 5

Hi experts! 

I am wondering if there are any recommendations about using global variables in single instance codeunits in Business Central. Generally, in modern object oriented programming defining and using global variablies should be avoided at all costs.

Are there any official guidelines for BC?

Here is an example where it is really tempting to use single instance codeunit global variables: I am using two subscribers - OnValidate Qty on Warehouse Receipt Line (1) and OnValidate Qty on Purchase Line (2). 

(1) should update values on both Warehouse Receipt Line and on Purchase Line.

(2) should update only values on Purchase Line.

The problem is that when I call (1), it triggers my code in subscriber (2) - I don't want to do that. I don't have a handled boolean here in parameters, as those are standard OnValidate triggers. 

What is the best approach? Is using single instance codeunit global variables a no-go here?

  • 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

    dynamicsuser.net/.../developers

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

    Thanks.

  • JAngle Profile Picture
    159 on at

    This is worth watching: https://youtu.be/rmdUXtGqYw8

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

#2
YUN ZHU Profile Picture

YUN ZHU 545 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 328 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans