Skip to main content

Microsoft Dynamics NAV 2016 - How to Use Trigger Events - Part 1

Hi All,

Recap - "Till Now We Have Discussed the Concept of Events and Subscription With an Example".
If you are missing the Story, Refer Table of Index For Events & Subscriptions.

Let's start it with What Microsoft Says About Events, Publisher and Subscribers.

> An event is the declaration of the occurrence or change in the application. An event is declared by a C/AL function, which is referred to as an event publisher function. An event publisher function is comprised of a signature only and does not execute any code.

> A publisher is the object that contains event publisher function that declares the event. The publisher exposes an event in the application to subscribers, essentially providing them with a hook-up point in the application. Publishing an event does not actually do anything in the application apart from making the event available for subscription.

> A subscriber listens for and handles a published event. A subscriber is a C/AL function that subscribes to a specific event publisher function and includes the logic for handling the event. When an event is raised, the subscriber function is a called and it code is run.

If you don't understand what above statement says don't worry I will make sure that all doubts get clear when we finish this series about Event, Publisher and Subscribers.

Types of Events In Microsoft Dynamics NAV 2016 - 

There are three different event types: Business, Integration, and Trigger events.

In This Article We Will talk about and see How we can use Trigger Events (The Simple One) - 

Points To Note -

> Trigger Events Only Works with Table and Page Object.
> Trigger Events are already Declared (No Declaration Required).
> For Trigger Events We only need to Subscribe It and Use It.

So Let's See how it Happens, first theoretically and then we will see a Demo for same -

What are my table functions which are released my Microsoft -

For Table - OnInsert, OnModify OnRename & OnDelete.
For Each Table Field - OnValidate & OnLookup.

When we Say Trigger Events it Means for each of the table function listed above (Except Field Lookup) there are Two Functions Added Like -

> OnBefore
> OnAfter 

Just to Clarify OnBefore Doesn't Mean that code execute Before Scope of Validate Start or OnAfter Doesn't Mean that Code Executes After Scopes of Validate Completed.

It's Just the Positional Parameters, Before Staring Standard Code or After Completing Standard Code, as shown below.

So Much Talk Let's start See Practical Scenario of Using Trigger Events -

1. Consider a Scenario Where your customer says That He Need A Solution as below -

> There will be a Table called Item Revision where I will store some extra Information about Item.
> That Should be accessible from Item Pages.
> When I Select a Item on Sales Order, a Field (Revision No.) Should get updated on Sales Line.
> The Field Should Flow to Posted Sales Invoices and Posted Sales Shipment.
> If I Delete a Item The Details Related to Item on New Table Should get Deleted.

We will cover some in this article and some in Next Articles.

So Let's start Customizing the Standard NAV W1 Objects for Same.

1. Create a Table & Page as below with ID 50000.

2. Now We Link This Page on Item Pages (List and Card) as usual Way.

3. Now We Require to Make Sure that whenever a Item Is Deleted Its Related Revision Details are also Deleted.

What we Used to Do, something Like This - OnDelete of Item Trigger Write a Custom Code as Below.

What We Should Do Now -

4. Create a Custom Codeunit 60000 Say MyCustomer Event Subscribers.

 > Navigate to Functions and Create a Function Say - ExecuteonDeleteofItem
 > Navigate to Properties of Function and set as below -
   > Event - Subscriber
   > EventPublisherObject - Table 27
   > EventFunction - OnAfterDeleteEvent (Say Yes to Confirmation Window).

5. Now You will see that you have created a New Subscriber Events.
6. Lets write the Same Code Here.

Let's Create a Item and Add Some Revision into the Item and Then Let's Delete the Item, To test the code written.

Hope the article helps you in understanding what are Trigger Events and how we use them. I know there are so many questions in your mind, do share your queries on Email or comment to this article.

If Required You can download the Objects for NAV 2016 W1 from OneDrive.

** File Name - Microsoft Dynamics NAV 2016 - Event Demo (Part 1 Objects)

In the Next article we will keep our focus still on the Trigger Events.

Stay Connected.

Saurav Dhyani


Popular posts from this blog

Welcome - Microsoft Dynamics 365!

Hi All, In this article we will discuss the new introduced member of Dynamics Family. Yes you heard it correctly, one more new member with Name "Microsoft Dynamics 365" . #ProjectMadeira   #Dynamics365

Send Mail with Attachment From Navision.

Hi all, We have seen how to save a report into PDF and how to send mail to a customer. Let's link these two post in one i.e. Mailing statement to a customer into PDF Format. This article is part of the Series. Please Refer  Table of Content here . If you have the old objects set let me brief you what I will be changing - 

MSDYN365BC - Data Upgrade To Microsoft Dynamics 365 Business Central on premises.

Hi Readers, We have already talked about the number of steps for upgrading to Business Central on Premises from different NAV versions. After that article, I received multiple requests for an article which list down steps for Data Migration. In this article, we will discuss steps of data migration to MSDYN365BC (on-Prem) from NAV 2017. For this article, I am considering a Cronus Demo Database without any customization. For an actual upgrade project, we will have to complete object merge using compare and Merge process. After the Merge Process, the next step is data migration. Let's discuss those steps. Direct Upgrade to Microsoft Dynamics 365 Business Central (on-Prem) is from following versions - 1. NAV 2015. 2. NAV 2016. 3. NAV 2017. 4. NAV 2018.