Create and send notifications

Completed

To create a notification, you need to define a variable of data type Notification. Some properties and functions that you can use on a variable of data type Notification are:

  • ID - Specifies an identifier for the notification.

  • Message - Specifies the content of the notification that appears in the UI.

  • Scope - Specifies the scope in which the notification appears. You don't have to specify this property because the default is LocalScope.

  • Send - Sends the notification to be displayed by the client.

  • AddAction - Adds an action on the notification. By using the AddAction property, you can run a specific method in another codeunit.

  • SetData - Sets a data property value for the notification.

  • GetData - Gets a data property value from the notification.

  • Recall - Recalls a sent notification.

If you specify an ID, you can recall this notification with the Recall function. Using an ID will also make sure that notifications with the same ID only appear once in the list.

To send a basic notification, you need to provide a message and use the Send function. Be careful with the Message function; you can't use placeholders like you would on a regular Message statement. If you want to use placeholders, you need to use the StrSubstNo function to replace placeholders with their values.

var 
   MyNotification: Notification;
begin
   MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
   MyNotification.Message('Here comes your notification text.');
   MyNotification.Send();
end;

You can also add an action to the notification. This feature allows the user to select the action and run a function in a codeunit. To send an action with a notification, use the AddAction function.

MyNotification.AddAction(Caption, CodeUnitID, MethodName);

The following sample changes the previous example by using the AddAction function.

var
    MyNotification: Notification;
    ShowDetailsTxt: Label 'Show details';
begin
    MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
    MyNotification.Message('Here comes your notification text.');
    MyNotification.AddAction(ShowDetailsTxt, Codeunit::"Notif Mgmt", 'ShowInfo');
    MyNotification.Send();
end;

The following example shows how to create a global function in another codeunit that has a Notification data type parameter. You can add code to the function for handling the action.

codeunit 50102 "Notif Mgmt"
{
    procedure ShowInfo(MyNotification: Notification)
    begin
        Page.Run(Page::"Customer List");
    end;
}

You can also pass data to the Codeunit function. Therefore, you can't pass the data as a parameter, but you need to use the SetData and GetData functions to add data to the notification. The SetData and GetData functions define the data as a key/value pair. The key/value pair can only accept text data types. If you want to pass a DateTime value or an Integer, you first need to format the value.

var
    MyNotification: Notification;
    ShowDetailsTxt: Label 'Show details';
begin
    MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
    MyNotification.Message('Here comes your notification text.');
    MyNotification.SetData('Created', Format(CurrentDateTime, 0, 9));
    MyNotification.SetData('CustomerNo', '10000');
    MyNotification.AddAction(ShowDetailsTxt, Codeunit::"Notif Mgmt", 'ShowInfo');
    MyNotification.Send();
end;

The GetData function retrieves the data again by using the key.

codeunit 50102 "Custom Notification Management"
{
    procedure ShowInfo(MyNotification: Notification)
    var
        Customer: Record Customer;
        CustomerNo: Text;
        Created: DateTime;
    begin
        CustomerNo := MyNotification.GetData('CustomerNo');
        Evaluate(Created, MyNotification.GetData('Created'));

        if Customer.Get(CustomerNo) then
            Page.Run(Page::"Customer Card", Customer);
    end;
}