Learn the basics of AL programming language
A codeunit is a container for application language (AL) code that you can use in many application objects. Typically, you will implement business logic in codeunits and call the codeunit from the object that needs to perform the specific logic.
Entering the tcodeunit shortcut creates the basic layout for a codeunit object when you use the AL Language extension in Visual Studio Code.
AL is the programming language that's used for manipulating data (such as retrieving, inserting, and modifying records) in a Business Central database. It is also used for controlling the implementation of the various application objects such as pages, reports, or codeunits.
With AL, you can create business rules to ensure that the data that is stored in the database is meaningful and consistent with the way that customers do business.
Generally, you'd write the code in codeunits instead of on the object on which it operates. This approach promotes a clean design and provides the ability to reuse code. It also helps enforce security. For example, users typically don't have direct access to tables that contain sensitive data, such as the General Ledger Entry table. They also don't have permission to modify objects. If you put the code that operates with the general ledger in a codeunit, give the codeunit access to the table, and give the user permission to run the codeunit, then you won't compromise the security of the table and the user can access it.
If you must put code on an object instead of in a codeunit, then put the code as close as possible to the object that it operates with. For example, put code that modifies records in the triggers of the table fields.
Reusing code helps make developing applications faster and easier. Moreover, if you organize AL code as suggested, your applications is likely to be less prone to errors. By centralizing the code, you won't unintentionally create inconsistencies by performing the same calculation in many places. For example, you perform in several triggers that have the same table field as their source expression. If you have to change the code, you could either forget about some of these triggers or make a mistake when you modify one of them.
When you create new objects, you will put AL code in the triggers of that object. Almost every object in Business Central contains triggers where you can add your AL code.
Triggers exist for the following objects:
Tables and table extensions
Table fields
Pages and page extensions
Reports
Data items
XMLports
Queries
Codeunits
The use of events is a proven and established programming concept that can ease application upgrades and limit, or even eliminate, the need for code modifications in customized applications because of application platform changes.
You can use events to design the application to react to specific actions or behaviors that occur. Events enable you to separate customized functionality from the application business logic. By using events in the application where customizations are typically made, you can lower the cost of code modifications and upgrades to the original application in the following ways:
Code modifications to customized functionality can be made without having to modify the original application.
Changes to the original application code can be made with minimal impact on the customizations.
Events can be used for different purposes, such as generating notifications when certain behaviors occur or the state of an entity changes. Events can also be used for distributing information and integrating with external systems and applications.