How to do it...

  1. Open your AL project in Visual Studio Code.
  2. In Explorer, create a new file named Customer Card Notification.al, and in Editor, create a new codeunit object:
codeunit 50108 "Customer Card Notification"
{

}
  1. In order to make the notification show, we must subscribe to OnAfterGetRecordEvent so that our notification will be evaluated when the customer record is retrieved from the database. 

Add the following code to the codeunit object to create the subscriber:

[EventSubscriber(ObjectType::Page, Page::"Customer Card", 'OnAfterGetRecordEvent', '', false, false)]
local procedure OnAfterGetRecordEvent(var Rec: Record Customer)
begin
HandleCustomerCardNotification(Rec."No.");
end;
  1. Now, let's create the function that shows the notification if the customer has no television shows defined:
local procedure HandleCustomerCardNotification(CustomerNo: Code[20])
begin
if CustomerNo = '' then
exit;

if not HasTelevisionEntries(CustomerNo) then
ShowCustomerCardNotification(CustomerNo);
end;
  1. We need to add the function to see if the customer has any television shows:
local procedure HasTelevisionEntries(CustomerNo: Code[20]): Boolean
var
CustomerTelevisionShow: Record "Customer Television Show";
begin
CustomerTelevisionShow.SetRange("Customer No.", CustomerNo);
exit(not CustomerTelevisionShow.IsEmpty());
end;
  1. Now, we can add the function that shows the actual notification:
local procedure ShowCustomerCardNotification(CustomerNo: Code[20])
var
CustomerCardNotification: Notification;
NotificationMsg: Label 'Customer %1 has no television shows.
Do you want to set some up?';
ActionYesTxt: Label 'Yes';
begin
CustomerCardNotification.Message := StrSubstNo(
NotificationMsg, CustomerNo);
CustomerCardNotification.Scope := NotificationScope::LocalScope;
CustomerCardNotification.SetData('CustomerNo', CustomerNo);
CustomerCardNotification.AddAction(ActionYesTxt,
CODEUNIT::"Customer Card Notification",
'OpenCustomerTelevisionShows');
CustomerCardNotification.Send();
end;

In the previous example, we define what the message will be to the user using the Message notification property. By setting Scope := NotificationScope::LocalScope, we're telling the system that the notification will appear on the current page that the user is working on.

NotificationScope::GlobalScope is available, but at the time of writing this book, it is not yet supported in Business Central. According to the Microsoft Docs site, it is slated for a future release.

Since we want the user to be able to interact with the notification, we added an action to the notification using the AddAction function. When the user clicks on this action within the notification, it will launch a function named OpenCustomerTelevisionShows in the Customer Card Notification codeunit.

The last piece of information that we add to the notification is the customer number. We need to store this piece of data within the notification itself so that we can use it later to open the list of television shows for the action. We do that by using the SetData function.

You can store multiple pieces of data within the notification simply by assigning a unique identifier to each value.

The last step in this function is to display the notification using the Send function.

  1. The final piece of code we need to add to our codeunit is the function that will be called when the user clicks on the notification action that we defined previously.

Add the following code to the codeunit:

procedure OpenCustomerTelevisionShows(CustomerCardNotification: Notification)
var
CustomerTelevisionShow: Record "Customer Television Show";
CustomerTelevisionShows: Page "Customer Television Shows";
CustomerNo: Code[20];
begin
CustomerNo := CustomerCardNotification.GetData(
'CustomerNo');
CustomerTelevisionShow.SetRange(
"Customer No.", CustomerNo);
CustomerTelevisionShows.SetTableView(
CustomerTelevisionShow);
CustomerTelevisionShows.Run();
end;

In the previous function, we use the GetData function to get the customer number from the notification that we previously stored there. 

Three things are important to remember when creating actions for notifications:

  1. It's testing time again! Press F5 to build and publish your application:
    1. Go to Customer List and find a customer that does not have any television shows defined.
    2. Click on the customer entry to open the CUSTOMER CARD page.

You should see a similar notification to this:

If you repeat the process for a customer that has television shows defined, then you should not see the notification.