AL, Business Central, Development Tips, Events, Extension Package, How To, Information, Modern Development Tool, Notification, Raise, Subscriber, Tip & Tricks, V2, Visual Studio Code

Notifications – Business Central

Today in this post we will discuss about Notification and how to implement.

First let us see, what are Notifications?

  • Non-intrusive message
  • Differs from the message initiated by the MESSAGE method.
  • Contextual
  • You can specify up to 4 actions
  • User can ignore and continue to work, doesn’t lock the user for immediate response.
  • Works with any client

Messages are modal, which means users are typically required to address the message and take some form of corrective action before they continue working. On the other hand, notifications are non-modal. Their purpose is to give users information about a current situation, but do not require any immediate action or block users from continuing with their current task.

Not-1

Tips for Create Notifications

  • Always use a unique ID, can be any GUID
  • ADDACTION points to a function
  • Arguments are case-sensitive literal
  • Functions must be global
  • Function must accept a Notification as parameter
  • Use SETDATA and GETDATA to exchange information between the sending and handling function.

Using Notification and NotificationScope data types and methods you can add code to send notifications to the users.

var

MyNotification : Notification;

Available methods are as below:

Method Description
MESSAGE Specifies the content of the notification that appears in the UI.

MyNotification.Message := ‘This will be shown as Notification’

SCOPE Specifies the scope in which the notification appears.

MyNotification.SCOPE := NOTIFICATIONSCOPE::LocalScope;

SEND Sends the notification to be displayed by the client.

MyNotification.SEND

ADDACTION Adds an action on the notification.

MyNotification.ADDACTION(‘Action 1’, CODEUNIT::”My Action Handler Codeunit”, “RunMyAction1’);

SETDATA Sets a data property value for the notification

MyNotification.SETDATA(‘CustomerNo’,Customer.”No.”);

GETDATA Gets a data property value from the notification.

MYNotification.GETDATA(‘CustomerNo’);

RECALL Recalls a sent notification.

Without going further any more in theory, let’s check how we can enable Notification.

Step – 1

Create a Codeunit to handle the Actions for Notification.

As usual we create a new Codeunit in our Extension Project.

codeunit 50101 NotificationRaiseAndHandel

Step – 2

Raise the Notification

You can Raise the Notification on Specific Page or can subscribe to the Events of that Page and write your code.

I am using the Subscription way to Raise my Notification.

For Demo purpose I am subscribing to OnOpen Event of the Page, in actual you need to decide from which action/event you wish to raise this Notification.

Code are self-explanatory, so no need special commentary.

  • I am checking for Company Info Setup we created in our earlier post if it is complete or not and then inform user about you Company Information is not completed and give him the option if he wish to complete that activity.

[EventSubscriber(ObjectType::Page, Page::”Sales Order”, ‘OnOpenPageEvent’,

”, true, true)]

local procedure CheckCompanyInfo(var Rec: Record “Sales Header”)

var

CompInfo: Record “Company Information”;

MyNotification: Notification;

begin

if compinfo.get then

if (compinfo.name <> ”) and (compinfo.”E-Mail” <> ”) then

EXIT;

MyNotification.Id := FORMAT(CreateGuid, 0, 9);

MyNotification.Scope := NotificationScope::LocalScope;

MyNotification.Message := ‘Company Information Incomplete.’;

MyNotification.AddAction(‘Open Company Information’, 50101,

‘DisplayCompanyInfoWizard’);

MyNotification.Send;

end;

  •  I am checking the Credit Limit of the customer if it is low, attracting attention towards it and giving him chance to update the Credit Limit.

   [EventSubscriber(ObjectType::Page, Page::”Sales Order”, ‘OnOpenPageEvent’,

”, true, true)]

local procedure CheckCreditBalance(var Rec: Record “Sales Header”)

var

Customer: Record Customer;

MyNotification: Notification;

Text001: TextConst ENU = ‘Balance Exceeds Credit Limit.’;

Text002: TextConst ENU = ‘Wish to change Credit Limit?’;

begin

Customer.Get(Rec.”Sell-to Customer No.”);

Customer.CalcFields(“Balance (LCY)”);

if Customer.”Balance (LCY)” > Customer.”Credit Limit (LCY)” then begin

MyNotification.Message(Text001);

MyNotification.Scope := NotificationScope::LocalScope;

MyNotification.SetData(‘CustomerNumber’, Customer.”No.”);

MyNotification.AddAction(Text002, 50101, ‘OpenCustomer’);

MyNotification.Send;

end;

end;

Step – 3

Handle the Action

Write the procedure which will respond to Actions.

   procedure DisplayCompanyInfoWizard(MyNotification: Notification)

var

CompInfo: Record “Company Information”;

begin

Page.Run(Page::CompanyInfoWizard);

end;

   procedure OpenCustomer(MyNotification: Notification)

var

CustomerNo: Text;

Customer: Record Customer;

CustomerCard: Page “Customer Card”;

begin

CustomerNo := MyNotification.GetData(‘CustomerNumber’);

if Customer.Get(CustomerNo) then begin

CustomerCard.SetRecord(Customer);

CustomerCard.RunModal;

end else begin

ERROR(‘Customer %1 not found.’, CustomerNo);

end;

end;

Here is the complete Code:

Not-2

Step – 4

Publish the Extension

To Publish choose AL:Publish from command Pallet.

Step – 5

Test the Solution.

Open the Sales Order Page and you will see the Message as shared above. Keep in mind this is just for demo, this is not the right place from where such Notifications will be raised, in real scenario you need to raise these Notifications from appropriate action/event. Now you know the basics can build your own logic around.

Will come up with some more Development Tips in my future posts.

Enjoy coding and learning. Take Care of yourself.

 

Advertisement
AL, Business Central, C/AL, Development Tips, Events, Extension Package, How To, Information, Modern Development Tool, Publisher, Raise, Subscriber, Tip & Tricks, V2, Visual Studio Code

Event driven Programming – Business Central

You can use events to design your application. Below are the benefits of using this model.

  1. You can lower the cost of code modifications and upgrades.
  2. You can customize functionality without modifying the original application.
  3. Your program will react to specific actions or behaviours of original application.

E-2

The following table describes all the different event types:

Event types Description
BusinessEvent Specifies the method to be business type event publisher.
IntegrationEvent Specifies the method to be integration type event publisher.
Global Global events are predefined system events.
Trigger Trigger events are published by the runtime.

You program events in the application to run customized behaviour when they occur.

E-1

What are Events?

A thing that happens. Event is declared by an AL method, which is referred to as event publisher function. Publisher method have only signature only and does not execute any code.

Publisher is the object that contains event publisher methods that declares the event. It serves as hook-up point in application, where subscribers use these points to extend the functionality, without even making any changes to the base application.

Only publishing an event do nothing in application, these events must be raised for subscribers to respond.

Especially Business and Integration type events must be published and raised, you need to create event publisher functions and add them to the objects manually.

Trigger events which occurs on Table & Page operations, are automatically published and raised by system at runtime, so no coding is required to publish them.

Subscriber is an AL method that subscribes to even publisher method, and logic to handle the event is implement.

E-3

Creating an event publisher method to publish business and integration events

Creating event publisher method is similar to other methods you define in AL. In addition some specific properties and few restrictions.

  • Can not include any code except comments.
  • You cannot define return values, variables or text constants.

You can define event publisher in any objects new or in existing objects and of any type of objects like codeunit, page or table.

If you define even as local then it will not be available for subscribers.

[IntegrationEvent(IncludeSender,GlobalVarAccess)]

local procedure MyProcedure()

begin

end;

[BusinessEvent(IncludeSender)]

local procedure MyProcedure()

begin

end;

Feel free to add as much of parameters and of any type as required. However it is advised not to include unnecessary parameters to Business events.

Raising Events

You need to modify the application to raise the event where ever it is needed. You call the event publisher method, same way you call any other methods in AL.

When the execution hits the evet publisher method, all event subscriber method that subscribe to the event are executed. Limitation will be you can not specify the order in which subscriber method will run, subscribers will be picked one at a time and in random order.

E-4

Subscriber Method

You can create new or use existing codeunits to define subscriber methods.

[EventSubscriber(ObjectType::Codeunit, Codeunit::, ‘OnSomeEvent’, ‘ElementName’, SkipOnMissingLicense, SkipOnMissingPermission)]

local procedure MyProcedure()

begin

end;

Add code to the method for handling the event.

Don’t worry at this point we will go through complete process programmatically in our next upcoming posts.

How to find which event to subscribe, and where to write our code.

Similar to earlier days we used Code Coverage, same way we have Event Recorder in Business Central.

Search for Event Recorder in RTC or alternatively you can launch from VS Code from Command Palate AL: Open Events Recorder.

Let’s look at a small example of finding Events.

I want to know what all events hit or available to subscribe when Sales-Order is Re-Opened.

Step-1: Open the Event Recorder and Click on Start.

E-5

Step-2: Perform Sales-Order -> Reopen

Step-3: Click on Stop.

E-6

Step-4: Scan from the list of events that you find suitable, to know how to subscribe to that event you can find AL Code. (Get AL Snippet)

E-7

All the recorded events display in the order they were called. The Event Recorder page provides information on the events that were raised including the details whether the raised events were trigger events or custom events. The custom events are either Business Events or Integration Events.

You can identify the Event types, additionally, you can discover which object types and methods raised the events with the details like calling methods, object types, and object names.

Readiness to Event:

  • Redesign your on prem to an event-based approach in C/AL.
  • This will prove to be best preparation for moving to VS Code AL extension.
  • You will be able to find any issues in your code that you need to refactor.
  • Next step will be to Lift your on prem product to VS Code AL extension.
  • Then extension can be published as a MSDY365 Business Central app.

Now you know about basics of Events. Understand Publisher, Raising Events and Subscribing to Events. Also how to trace and find suitable events to subscribe for your customization over base application.

Before we end the post let’s have a Recap to Events

Publish:

  • Announcement by the application
  • Function without code
  • Exposes the event to the outside

Raise:

  • Specifies exactly when the event happens
  • Call to the Publisher Function

Subscribe:

  • React to the event
  • Must be in codeunit, tableextension or pageextension.

Note: Raise order specified in code, Subscribe order undefined.

We will look into practical approach in our next post.