Header Ads Widget

Top Picks

6/recent/ticker-posts

Events in Business Central - When to use events - types of events in business central - implementing event in business central

Multi-purpose Buttons

Introduction


Using events, you may program the app to respond in many ways to user activity. Separating user-defined features from the business logic of an application is made possible through the use of events. 


You can reduce the price of code updates and upgrades by making use of events in the application at the points where customizations are commonly made.


In precise

  • Customized functionality code can be updated independently of the base app.
  • It is possible to make modifications to the original program code with little to no effect on the customizations.

Use of Events

  • Sending alerts when a predefined event occurs or the state of an entity changes
  • Sharing data with other systems
  • Connecting with third-party programs.

Types of Events 

  1. BusinessEvent - Describes how to post events of a business-type.
  2. IntegrationEvent - Describes how to broadcast internal events.
  3. InternalEvent - Describes the procedure to use as an internal event publisher
  4. Global - System events with a predetermined global scope.
  5. Trigger - Trigger events are published by runtime.


Three Major Participants in Events

  1. Event
  2. Publisher
  3. Subscriber

Event: 

  • Announcement of the occurrence or modification to the application
  • An event publisher function, which is declared via an AL method.
  • The event publisher method merely has a signature and doesn't run any code.

Publisher:

  • A publisher is an object that uses the event publisher function to define an event.
  • The publisher makes an application event available to the subscribers, giving the latter a point of entry into the application.
  • While it may be tempting to think otherwise, publishing an event does nothing more than make it accessible by subscription.
  • Subscribers can only react once the event has been triggered.
  • Developers must add code (the event publisher function) to their apps that interact with publishers in order to make an event happen.
  • Event publisher functions must be developed and these capabilities must be manually added to objects in order for events of the business and integration kinds to be explicitly published and raised.
  • Trigger events, on the other hand, happen on table and page activities and are implicitly published and raised by the system during runtime. Thus, they can be released without the need for any coding.

Subscriber:

  • An event may be publicised, after which a subscriber may hear it and take appropriate action.
  • An event handler (or subscriber) is an AL method that has subscribed to a certain event publisher method.
  • Every time an event is created, the subscriber method's code is run.
  • A subscriber allows third-party developers to integrate with the heart of an app without making custom alterations to the source code.
  • Event subscribers can be used by any Dynamics 365 solution vendor, including Microsoft.
  • A single event publisher method can have numerous subscribers. A publisher, however, is in the dark as to how many people are actually subscribing.
  • Both publishers and subscribers can work from various portions of the app.


How to implement an event

How to implement an event

Publish the event

  • For business and integration events, it is advised to identify a specific method in an application object as an event publisher method.
  • Trigger events are published by the system automatically, so this step is unnecessary.

Code

// The subsequent code generates a codeunit that publishes the event
"OnAddressLineChanged."

codeunit 50102 MyEventPublishersS
{
    [IntegrationEvent(false, false)]
    procedure OnAddressLineChanged(line: Text[100])
    begin
    end;
}

Raise the Event

  • Incorporate a call to the event publisher mechanism.
  • Trigger events are automatically raised by the system, therefore this step is unnecessary.

Code

// The OnAddressLineChanged event is raised by the following code, which extends
the Customer Card page.
// when the Address field is changed.
//PHASE 2: RAISING EVENT
pageextension 50110 MyCustomerExt extends "Customer Card"
{
    layout
    {
        modify(Address)
        {
            trigger OnBeforeValidate();
            var
                Publisher: Codeunit MyEventPublishersS;
            begin
                Publisher.OnAddressLineChanged(Rec.Address);
            end;
        }
    }
}

Subscribe to the event

  • Create a consumer-side subscriber method or methods that react to published events.

Code

// The code unit below establishes an event subscriber.
// This line declares the 'CheckAddressLine' event subscriber, which watches for the OnAddressLineChanged event.
// The event subscriber displays a message in the client when '+' is used in the **Address** field.

//PHASE 3: SUBSCRIBING THE EVENT
codeunit 50103 MySubscriberss
{
    //Set the event subscribers to join the event automatically
    EventSubscriberInstance = StaticAutomatic;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"MyEventPublishersS", 'OnAddressLineChanged', '', true, true)]
    procedure CheckAddressLine(line: Text[100]);
    begin
        begin if (STRPOS(line, '+') > 0)
            MESSAGE('Can''t use a plus sign (+) in the address [' + line + ']');
        end;
    end;
}


Save all the above codes as a single program.

Launch the customer card page.


Check here for the video instructions: 

Post a Comment

0 Comments

Youtube Channel Image
goms tech talks Subscribe To watch more Tech Tutorials
Subscribe