Introduction

Completed

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 codeunit contains only one trigger: OnRun. This trigger is always available and is implemented when you run a codeunit.

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 five extra triggers.

  • Test - This subtype enables you to write unit test functions. You don't create normal functions in this codeunit because it can only run during unit testing.

  • TestRunner - This subtype runs one or more Test codeunits.