How to add custom code using events and ship it with extensions

Hi guys, On request of some of the readers of my blog today I am going to discuss another cool topic “Adding custom code into existing objects and ship them with extension”.  This is the continuation of my last article which is about Getting started with Extensions in Dynamics NAV. In that article, I talked about how to modify existing objects and ship them with extensions. In today’s topic, I will talk about how to ship custom code with the extensions.

Prerequisite

To add Custom code in the existing objects you should know about the concept of events in Dynamics NAV. If you don’t know about events you can follow my post here. Furthermore, you should know about the basics of creating extensions in Dynamics NAV. If you don’t know about how to create/publish/install extension you can read my post here, I have discussed every step in detail.

Scenario

Suppose we want to create an extension whose functionality is whenever new customer is added in Dynamics NAV, this new customer is also inserted into my table.

To develop this extension we need to create a custom table which holds the data of a new customer inserted into Dynamic NAV. In addition to this, we will also write a codeunit in which we write a code to add the customer into custom table.

Steps

  1. Open the Development Database in the Dynamics NAV environment.
  2. Create a table 50005 (My Customers) and add the fields like No., Name , Email into that table.1
  3. Compile and save the table.
  4. Create a list page for the table 50005 “My Customers” table and attach this page in the page actions of the customer list page.
  5. Create a codeunit e.g. 50000.
  6. In the Globals add two functions, OnNavAppUpgradePerDatabase and OnNavAppUpgradePerCompany ,  and set the Local property of these functions to No. These two functions are used when you release an update of your extension. This is why they are mandatory by Dynamics NAV. If you don’t include them Dynamics NAV will generate an error when you are publishing your extension.
  7. Now create a function CreateCustomerInMyCustomerTableOnAfterModifyEvent.
  8. Go to the function properties and set the properties as:
    1. Event: Subscriber
    2. EventPublisherObject: Table Customer
    3. EventFunction: OnAfterModifyEvent2
  9. Close the property window and save the codeunit.
  10. Now go to the Locals of your function CreateCustomerInMyCustomerTableOnAfterModifyEvent and define a variable of type record to insert new item in My Customers Table.3
  11. Now write the code in your function.MyCustomer.INIT;MyCustomer.”No.” := Rec.”No.”;

    MyCustomer.”Customer Name” := Rec.Name;

    MyCustomer.”Customer Email” := Rec.”E-Mail”;

    IF NOT MyCustomer.INSERT THEN

    MyCustomer.MODIFY;4

  12. Once we done with the development, its time to publish and text our extension.
  13.  Export all the objects of the development database in the .txt file.
  14. Run the powershell script which I have created to publish and install the extension. You can download this script from here . If you want to know how to use this script you can refer to my post here.
  15. Once the extension has been installed, you can go to Windows client and add the new customer in Dynamics NAV. You will observe when you add the customer in Dynamics NAV this customer also add into your table.

56

Check My Customers table now

7

Congratulations! You have created your first extension with custom code in Dynamics NAV. If you like my post, don’t forget to share it. Feel free to ask if you have any query / question. 🙂

To download the demo files: Click here

To download the powershell script: Click here

Advertisements

Author: zohaib.ahmed

I am an IT professional, a GEEK by nature who loves to get his hands dirty by experiencing new technologies in the domain of computer science. I am doing development in different domain since last 3 years. Right now, I am working as a Dynamics NAV technical consultant.