Application object triggers
Trigger is a piece of code that is executed in response to some external action. All objects in NAV, except Menu Suite, have a set of triggers that can be programmed to respond to certain user's or system actions. For example, when a page with data is displayed on the screen, a sequence of triggers are fired in the application.
OnInit
: Page object is initializedOnOpenPage
: Page is displayedOnAfterGetRecord
: Table record displayed on the page is read from the databaseOnAfterGetCurrRecord
: Table record currently selected is read from the database
There are other triggers reacting to UI elements, data modifications or to external events from .NET components. We will delve deeper into different types of objects and corresponding triggers later in the book. Now let's create a code module (called a codeunit in NAV) with a single trigger that fires when the object is executed.
How to do it...
- In the left column of the object designer, click on the codeunit object.
- Click the New button in the bottom part of the Object Designer form. A new codeunit object is created.
- Each object in NAV must have a unique name and number that are assigned to the object when it is saved in the database. Click File | Save and fill in the ID and Name fields in the Save As dialog. Leave the Compiled option checked:
- The new codeunit has two system-created triggers. Position the cursor in the empty line below Documentation and write a short description of your new codeunit. For example,
This is my first NAV codeunit
: - Move the cursor to the
OnRun
trigger and enter a line of code that will be executed when the trigger fires:MESSAGE('Codeunit OnRun trigger');
- Save the codeunit and close the editor window.
- In the object designer, select the codeunit
50000
and click the Run button located under the list objects. This action will start the role-tailored client and execute the codeunit. Execution of the codeunit fires itsOnRun
trigger. When run, it will show a message box with the codeunit OnRun trigger in it. - In the Object Designer window, create another codeunit, save it with ID
50001
and name itNAV Codeunit Runner
. - Write a line of code in the
OnRun
trigger that will invoke the first codeunit:CODEUNIT.RUN(CODEUNIT::"First NAV Codeunit");
- Close the code editor and run codeunit
50001
from the object designer. The same message box with the codeunit OnRun trigger will be shown.
How it works...
In most cases all triggers supported by an object are available in the C/AL code editor as soon as the new object is created. There are several exceptions when a trigger must be explicitly declared as a function having specific signature, but these triggers are outside the material covered in this chapter. All application triggers we are going to work with will be created by the C/SIDE without developer's intervention.
All NAV objects that can execute C/AL code (that is, all objects except Menu Suite), have the Documentation section. This object section is often referred to as a trigger, and looks like a function in the code editor, although it is never executed. Documentation is used to comment the object - usually comments applicable to the object in general are placed here.
Codeunit objects support only one trigger OnRun
that is called when the object is executed. In Step 7 we run the codeunit
manually from the object designer. In the steps, Step 8 to Step 10, the same trigger fires when execution of the codeunit is initiated from another codeunit's OnRun
trigger. This way, triggers can be chained, when the execution of an object can cause another trigger to fire.
To run the codeunit we use the system function CODEUNIT.RUN
which takes the codeunit ID as the parameter. It can simply be a number (50000
). Or we can refer to the codeunit by its name, making the code more human-readable.