Skip to main content

MSDYN365BC - Futuristic CAL Development - UnBoxing Database Trigger Event.

Hi Readers,

Last article we subscribed a Trigger Events. As promised in this post we will understand Database Trigger Events in Base C/AL Code.

If you are new to this series then Please Refer Table of Index.

With this article, I will try to clarify all doubts and questions about  Database Trigger Events.

This will help Developers to subscribe to Database Trigger Events. Let's discuss that in details.


What are Trigger Events?
Unlike business and integration events which must be programmed, trigger events are predefined events. Trigger events are published by the runtime and they cannot be raised programmatically. There are two types of trigger events: database trigger events and page trigger events.

Who Can Publish Database Trigger Events?
Microsoft, Solution ISV & Microsoft Partners Developers can Define Integration Events to provide a Hook for external parties without modifying the code.
But unlike Integration Events nothing needs to done, Developer Environment automatically publishes Database Trigger Events.

How Database Trigger Events Published?
Whenever we add a field in a table or add a new table, the developer environment will automatically publish Database Trigger Events.

What Objects Will Publish Trigger Events?
Publish(add) new Database Trigger Events are automtically added in Tables.

How can we Publish Trigger Events?
A trigger Event will automatically added for -

  • Every Table Trigger.
  • One Field Trigger in Table.

Let's Understand this with an Example of new table that we added in this Series "Expense".

Database trigger events - 

Each Navision / Business Central Table have 04 Table Triggers -
  • OnInsert.
  • OnModify.
  • OnDelete.
  • OnRename.
And Each Navision Table Field have two triggers -
  • OnValidate.
  • OnLookup.

Expense Table have Three Fields as shown below with triggers.



** Database Trigger Events Provides hooks for all Table Triggers and only OnValidate for each field.
** Database Trigger Events are not supported/available for OnLookup Trigger.
** Database Trigger Events are available in pair - OnBeforeEvent and OnAfterEvent.

What Objects can be used for Trigger Events?
Only Codeunit are allowed for Subscribing Trigger Events also.

How can we Subscribe Database Trigger Events?
Three Parts for Subscribing a Published Database Trigger Events.

1. Create a New Function.
Define it as Subscriber & Select Published Event.
Write Custom Code that is required.

Let's try to subscribe Expense table Database Trigger Events.
** This part is not the part of the solution that we are building it is just sample code.

Let's create a new Codeunit to see what Trigger Events are available for Expense Table.

Create a new Codeunit and then Create a New Function - OnBeforeValidateExpenseCode and then Define it as a Subscriber, as shown below.



2. Next, we need to tell Where is Published Event with Following Details -

EventPublisherObject - Object Type and Id where Event is Published.
EventFunction - Published Event Name that we want to subscribe.


3. Next, as we have multiple fields in the table, we also need to define for which field we want to Subscribe to. For that set the property EventPublisherElement and select the field that we want to trigger it for.


With this, our Subscriber is ready and let's see what parameters subscriber provides.

As you can see below we have - Current Record (rec), Previous Record (Xrec) and the Field Number (CurrFieldNo) we will be able to write code that we want to call when Validate trigger for Code field is called in Expense table.



** Remember for table triggers we don't need to specify EventPublisherElement as those triggers are field independent.
** EventPublisherElement is only available for OnBeforeValidate and OnAfterValidate EventFunction type.

Now let's see why we said earlier that Database Trigger Events are available in Pairs.

As you see in below image (pairs in the same color), all table triggers and field triggers are available in pair with OnBefore and OnAfter.


Why Database Trigger Events are available in Pairs and when they are called in code execution?

OnBefore -  Will get invoked/triggered before any standard code written on that trigger.
OnAfter - Will get invoked/triggered after all standard code written on that trigger is executed.

Below image should clear that doubt -



Last but not Least -  What if my custom code is somewhere in middle?
That is not possible to be evented and I call it as Anti-Pattern. There are so many examples of Anti-Patterns and the only way going forward is to rewrite those Anti-Patterns in New Methodology.

Hope it makes sense and now you know about Database Trigger Events.

Next article, we will Unbox Page Trigger Events.

Stay Tuned for More!.

Regards,
Saurav Dhyani
www.sauravdhyani.com

Comments

Post a Comment

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

RTC Report It is not possible to instantiate the Visual Studio bridge.

Hi all, As a Navision developers I have Multiple Versions of Navision running in single Machine. As discussed Earlier in the post how to run multiple Version of RTC in single machine. So my machine have following details for RTC Versions - NAV 2009 R2    - is installed. NAV 2009 SP1  - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 SP1 IN\ NAV 2009         - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 IN\ This approach has been working great for execution of Classic and RTC Clients. However, after installing Dynamics NAV 2009 R2, if i tried to view the Layout for an NAV 2009 SP1 Report i was getting the following error: ---------------------------  Microsoft Dynamics NAV Classic  ---------------------------  It is not possible to instantiate  the Visual Studio bridge.  ---------------------------  OK    --------------------------- After searchi...

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.