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 -
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 -
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
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.
- 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.
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.
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
Nice post!
ReplyDeleteWelcome @Jon.
DeleteThank you so much Saurav.
ReplyDeleteWelcome @Navtech.
Delete