Introduction
In this module, you'll learn about the Codeunit object. Along with tables and pages, a codeunit is a core object of the Microsoft Dynamics 365 Business Central application.
Codeunits group different procedures into one object. These procedures can be accessed from within other procedures and triggers on pages, tables, and other codeunits.
A codeunit contains the following elements:
Triggers
Functions
Variables
Properties
Statements
A normal codeunit contains only one trigger: OnRun. This trigger is always available and is implemented when you run a codeunit. Install, upgrade, and test runner codeunits can also have subtype-specific triggers.
By using the code snippets (tcodeunit) for creating a codeunit with Visual Studio Code, the OnRun trigger is automatically generated.
Access property
You can use the Access property on the codeunit to specify your codeunit as public or internal. A public codeunit can be called within the same extension but also from within other extensions. An internal codeunit can only be accessed from within the same extension.
Subtype property
The Subtype property has five available values:
Normal - The default value for every new codeunit. This subtype is a regular codeunit. It has only one trigger: OnRun.
Install - This type of codeunit only runs during the installation of the extension package. This subtype provides access to two extra triggers.
Upgrade - This type of codeunit only runs during the upgrade process of an extension package. This subtype gives access to six extra triggers.
Test - This subtype enables you to write unit tests. Test codeunits can contain test methods, handler methods, and normal methods for structuring reusable test code.
TestRunner - This subtype runs one or more Test codeunits.