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

BC 21 and Higher - PowerShell Cmdlet (Replacement of Business Central Administration).

Hi Readers, As discussed in last article about deprecating of Business Central Administration, there are few common actions that we use in administration till Business Central 20. For our on-prem customers, we will still require doing activities. As Microsoft suggest we need to start using PowerShell cmdlet.    Let's see how to do those via PowerShell, or Administration Shell. I will be keep adding commands as you comment to this article.

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.