Friday, November 20, 2015

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.

Regards,
Saurav Dhyani
saurav-nav.blogspot.com

No comments:

Post a Comment