Skip to main content

Notifications

Event Handled pattern.

Jalmaraz Profile Picture Jalmaraz 667

The true challenge.

In new Visual Studio Code environment there are many challenges, but the main challenge is change to events architecture IMO. Isn´t possible to change the app core and all change of its behavior be done subscribing to existing events.

From subscribers to publishers.

But we are doing our own apps too and we could think about other programmers trying to change their behavior too, without accessing our core code: we can publish events for third party consumers of our applications.

Breaking news?

Mark Brummel proposed the pattern https://markbrummel.blog/2015/11/25/the-handled-pattern/  at the end of 2015. So, this pattern is at least 3 years old. But the new stuff is that Microsoft is using it in his core code, and we can find examples in standard application.

Handled flag: Did Someone process this before?

In page 42 Sales Order, we can find this code in statistics action:
Statistics - OnAction()
  OnBeforeStatisticsAction(Rec,Handled);
  IF NOT Handled THEN BEGIN
    OpenSalesOrderStatistics;
    SalesCalcDiscountByType.ResetRecalculateInvoiceDisc(Rec);
  END
 
Publisher raise an event with a switch called Handled. If a subscriber changes the flag to true, sales statistics page display will be skipped.

The subscriber side.

In the example below, we subscribe to stats action in sales order page and check if order is released: if not released we warn that statistics could not be so accurate and give the user the option of skip stats page till later. Another good feature could be replacing stats page by our own stats page.
HandledPat.png
To make this new feature, we create a new Codeunit and put this subscription code inside:
    [EventSubscriber(ObjectType::Page, page::"Sales Order", 'OnBeforeStatisticsAction', '', false, false)]
    local procedure CheckReleased(SalesHeader: Record "Sales Header"; var Handled: Boolean)
    begin
        if Handled then
            exit;
        with SalesHeader do begin
            if Status = Status::Released then
                exit;
            Handled := not Confirm(ConfShowStatsCaption);
        end;
    end;

Use Recap: publisher side (page 42). Microsoft standard code.

We publish the event with Handled flag included
[BusinessEvent(false)]
local procedure OnBeforeStatisticsAction(VAR SalesHeader : Record "Sales Header";VAR Handled : Boolean)   
 
If some subscriber has switched Handled to true, we skip further process:
  OnBeforeStatisticsAction(Rec,Handled);
  IF NOT Handled THEN BEGIN
    OpenSalesOrderStatistics;
Not all the process could be skipped: we must decide when is good for the business to allow if further logic could be skipped.

Use Recap: subscriber (our own Codeunit).

First step is to check if event is already handled:
        if Handled then
            exit;
Then we can assign Handled variable with our own logic:
            Handled := not Confirm(ConfShowStatsCaption);
Warning: be careful with the declaration of Handled flag: it´s very important to take it by reference if you want to change it later (I mean var clause before):
    [EventSubscriber(ObjectType::Page, page::"Sales Order", 'OnBeforeStatisticsAction', '', false, false)]
    local procedure CheckReleased(SalesHeader: Record "Sales Header"; var Handled: Boolean)
 

Comments

*This post is locked for comments