Understanding C/SIDE Codeunits

A codeunit is a container for C/AL code that you can use in many application objects. In simple applications, you can place the code in the object that calls the functions. For example, if you use C/AL code in forms, you can store the code in the form. However, as your application grows in both size and complexity, you will often find that you use the same functions more than once in many different objects. Instead of declaring the same functions repeatedly, you can use a codeunit to define them once.

What You Can Define in Codeunits

In codeunits, you can define the following:

  • Functions   A function is a sequence of C/AL statements that you define to create new functionality.

  • Local Variables   Within each function you can define variables whose scope is limited to the function in which they are defined. These are known as local variables.

  • Global Variables   A global variable is a variable whose scope covers all the functions in the codeunit.

  • Temporary Tables   A temporary table is a table that is not stored in the database. Temporary tables are used mainly as structured variables that hold data temporarily while you work on it.

Codeunit Sections

Each function that you add to a codeunit is shown in a separate section when you view the file in the C/AL Editor. Every codeunit contains two default sections called Documentation and OnRun. In the Documentation section, you can add descriptive information, such as the purpose of the codeunit or a version number. In the OnRun section, you can include code that you want to be executed when the codeunit is run.

In addition to being a container for functions that can be run individually, a codeunit can itself be run by using <Codeunitname>.Run. When you run a codeunit, the code in the OnRun section of the codeunit is executed.

Community Additions

ADD
Show: