Friday, February 15, 2019

MSDYN365BC - Futuristic CAL Development - UnBoxing Page Trigger Event.

Hi Readers,

Last article we unboxed Database Trigger Events. As promised in this post we will understand Page 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  Page Trigger Events.

This will help Developers to subscribe to Page 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 Page Trigger Events?
Microsoft, Solution ISV & Microsoft Partners Developers can Define Page Trigger Events to provide a Hook for external parties without modifying the code.
But unlike Integration Events nothing needs to done, Developer Environment automatically publishes Page Trigger Events.

How Page Trigger Events Published?
Whenever we add a new page, add a field in a page and add an action in a page, the developer environment will automatically publish Page Trigger Events.

What Objects Will Publish Page Events?
Publish(add) new Page Trigger Events are automatically added in Pages.

How can we Publish Page Trigger Events?
A page trigger Event will automatically be added for -
  • Few Page Trigger (Not All).
  • One Page Field Trigger in Table (Not All).
Let's Understand this with an Example of a new page that we added in this Series "Expenses".

Page trigger events - 
Each Navision / Business Central Table have the following Page Triggers -
  • OnInit
  • OnOpenPage
  • OnClosePage
  • OnFindRecord
  • OnNextRecord
  • OnAfterGetRecord
  • OnNewRecord
  • OnInsertRecord
  • OnModifyRecord
  • OnDeleteRecord
  • OnQueryClosePage
  • OnAfterGetCurrRecord
** Highlighted in Red are not available as an event.

Each Navision Page Field have the following triggers -
  • OnValidate
  • OnLookup
  • OnDrillDown
  • OnAssistEdit
  • OnControlAddIn
** Highlighted in Red are not available as an event.

And, Each Navision Page Action Field have the following triggers -
  • OnAction
Expenses Page have Three Fields as shown below with all page triggers.



** Page Trigger Events Provides hooks for few Page Triggers and only OnValidate for each field.
** Page Field and Action Trigger Events are available in pair - OnBeforeEvent and OnAfterEvent.

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

How can we Subscribe Page Trigger Events?
Two or Three Parts for Subscribing a Published Page Trigger Events.

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

Let's try to subscribe Expense Page 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 Page.

Create a new codeunit and then Create a New Function - OnInsertExpenseRecord 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. (Page Expenses)
EventFunction - Published Event Name that we want to subscribe. (OnInsertRecordEvent)



As we are subscribing a Page Trigger Event then we don't have to specify the field and even that option is not available.

As you can see below we have - Current Record (rec), Previous Record (Xrec), BelowXrec and the AllowInsert we will be able to write code that we want to call when Insert trigger for Page is called in Expense Page.


Now let's Repeat the same steps but this time try to subscribe Page Action Event.

Expense Page has a Page Action - WhereUsed, as shown below.


Now add a new function in Subscriber Codeunit - OnAfterWhereUsedAction and define its properties as shown below.



Now to see which one to use below will image will explain which trigger will fire when.


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 Page Trigger Events.

Next article, we will Discuss How should we use HOOKS when events do not exist or customer is in a version which does not support Events.

Stay Tuned for More!.

Regards,
Saurav Dhyani
https://saurav-nav.blogspot.com

No comments:

Post a Comment