Skip to main content

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!.

Saurav Dhyani


Popular posts from this blog

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 - 

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.

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.