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.
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.
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.
- Open the Development Database in the Dynamics NAV environment.
- Create a table 50005 (My Customers) and add the fields like No., Name , Email into that table.
- Compile and save the table.
- Create a list page for the table 50005 “My Customers” table and attach this page in the page actions of the customer list page.
- Create a codeunit e.g. 50000.
- 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.
- Now create a function CreateCustomerInMyCustomerTableOnAfterModifyEvent.
- Go to the function properties and set the properties as:
- Event: Subscriber
- EventPublisherObject: Table Customer
- EventFunction: OnAfterModifyEvent
- Close the property window and save the codeunit.
- Now go to the Locals of your function CreateCustomerInMyCustomerTableOnAfterModifyEvent and define a variable of type record to insert new item in My Customers Table.
- 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
- Once we done with the development, its time to publish and text our extension.
- Export all the objects of the development database in the .txt file.
- 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.
- 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.
Check My Customers table now
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