Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Extending Microsoft Dynamics NAV 2016 Cookbook

You're reading from   Extending Microsoft Dynamics NAV 2016 Cookbook Extend Dynamics NAV 2016 to win the business world

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786460608
Length 458 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alexander Drogin Alexander Drogin
Author Profile Icon Alexander Drogin
Alexander Drogin
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Writing Basic C/AL Code FREE CHAPTER 2. Advanced C/AL Development 3. Reporting and Data Analysis 4. .NET Interoperability in C/AL 5. Extending C/AL with COM Components 6. SharePoint Integration 7. Control Add-ins 8. Web Services 9. Events and Extension Packages 10. PowerShell

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 initialized
  • OnOpenPage: Page is displayed
  • OnAfterGetRecord: Table record displayed on the page is read from the database
  • OnAfterGetCurrRecord: 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...

  1. In the left column of the object designer, click on the codeunit object.
  2. Click the New button in the bottom part of the Object Designer form. A new codeunit object is created.
  3. 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:

    How to do it...

  4. 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:

    How to do it...

  5. 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'); 
    
  6. Save the codeunit and close the editor window.
  7. 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 its OnRun trigger. When run, it will show a message box with the codeunit OnRun trigger in it.
  8. In the Object Designer window, create another codeunit, save it with ID 50001 and name it NAV Codeunit Runner.
  9. Write a line of code in the OnRun trigger that will invoke the first codeunit:
            CODEUNIT.RUN(CODEUNIT::"First NAV Codeunit"); 
    
  10. 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.

You have been reading a chapter from
Extending Microsoft Dynamics NAV 2016 Cookbook
Published in: Jan 2017
Publisher: Packt
ISBN-13: 9781786460608
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime