Book Image

Extending Microsoft Dynamics NAV 2016 Cookbook

By : Alexander Drogin
Book Image

Extending Microsoft Dynamics NAV 2016 Cookbook

By: Alexander Drogin

Overview of this book

Microsoft Dynamics NAV is an enterprise resource planning (ERP) software suite for organizations. The system offers specialized functionality for manufacturing, distribution, government, retail, and other industries. Its integrated development environment enables customizations with minimal disruption to business processes. The book starts explaining the new features of Dynamics NAV along with how to create and modify a simple module. Moving on, you will learn the importance of thinking beyond the boundaries of C/AL development and the possibilities opened by with it. Next, you will get to know how COM can be used to extend the functionalities of Dynamics NAV. You’ll find out how to extend the Dynamics NAV 2016 version using .NET interoperability and will see the steps required to subscribe to .NET events in order to extend Dynamics NAV. Finally, you’ll see the cmdlets available to manage extension packages. By the end of the book, you will have the knowledge needed to become more efficient in selecting the extending methods, developing and deploying them to the Dynamics NAV, and practicing the best practices.
Table of Contents (17 chapters)
Extending Microsoft Dynamics NAV 2016 Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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:

  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:

  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.