Programming in AL
AL is the programming language that is used for manipulating data (such as retrieving, inserting and modifying records) in a Dynamics 365 Business Central and Dynamics NAV database, and controlling the execution of the various application objects, such as pages, reports, or codeunits.
AL tools functionality is supported in Dynamics 365 Business Central and Dynamics NAV, but in some cases, there are limitations. These limitations are marked clearly in the specific topic.
With AL, you can create business rules to ensure that the data which is stored in the database is meaningful and consistent with the way customers do business. Through AL programming, you can:
- Add new data or transfer data from one table to another, for example, from a journal table to a ledger table.
- Combine data from multiple tables into one report or display it on one form or page.
Where to write AL code
Almost every object in Dynamics 365 Business Central and Dynamics NAV contains triggers where you can add your AL code. Triggers exist for the following objects:
You can initiate the execution of your AL code from the following:
- Any object that has an instantiation of the object that contains AL code. An example of an instantiation is a variable declaration.
If the AL code is in a
local method, then you cannot run it from another object.
Guidelines for placing AL code
We recommend the following guidelines for AL code:
In general, put the code in codeunits instead of on the object on which it operates. This promotes a clean design and provides the ability to reuse code. It also helps enforce security. For example, typically users do not have direct access to tables that contain sensitive data, such as the General Ledger Entry table, nor do they have permission to modify objects. If you put the code that operates on the general ledger in a codeunit, give the codeunit access to the table, and give the user permission to execute the codeunit, then you will not compromise the security of the table and the user will be able to access the table.
If you must put code on an object instead of in a codeunit, then put the code as close as possible to the object on which it operates. For example, put code that modifies records in the triggers of the table fields.
Reusing code makes developing applications both faster and easier. More importantly, if you organize your AL code as suggested, your applications will be less prone to errors. By centralizing the code, you will not unintentionally create inconsistencies by performing the same calculation in many places, for example, 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.