D365 Business Central : Create Notifications in BC

Notifications

What is a notification ? Notifications are non-intrusive information to give users information about a current situation, but do not require any immediate action or block users from continuing with their current task. For example, you could have a notification that a customer’s credit limit is exceeded or not enough inventory or overdue balance, etc.

Using notification is a great non-intrusive way to inform the user. To do simple notification is quite easy. Let’s first look at the available methods.

MethodDescription
MessageSpecifies the content of the notification that appears in the UI.
ScopeSpecifies the scope in which the notification appears.
SendSends the notification to be displayed by the client.
AddActionAdds an action on the notification.
SetDataSets a data property value for the notification
GetDataGets a data property value from the notification.
RecallRecalls a sent notification.

Let’s try creating a few samples now.

Sample 1 : Create notification to inform user if the Posting Date is not today.

tableextension 50100 "My Sales Header" extends "Sales Header"
{
    fields
    {
        modify("Posting Date")
        {
            trigger OnAfterValidate()
            var
                MyNotification: Notification;
                PostingDateTxt: Label 'Posting Date is not today.';
            begin
                If Rec."Posting Date" <> Today() then begin
                    MyNotification.Message(PostingDateTxt);      
                    MyNotification.Scope(NotificationScope::LocalScope);
                    MyNotification.Send();
                end;
            end;
        }
    }
}

If we try to enter the posting date on future date, we will get the notification.

Nice. Now what happen if you try to change the posting date a few times outside of today’s date ? You will get multiple notifications which is not really nice.

Sample 2 : Create notification to inform user if the Posting Date is not today. However, remove old notification before sending a new one.

In order to do this, you need to assign a GUID to the notification and recall it if exists.

tableextension 50100 "My Sales Header" extends "Sales Header"
{
    fields
    {
        modify("Posting Date")
        {
            trigger OnAfterValidate()
            var
                MyNotification: Notification;
                PostingDateTxt: Label 'Posting Date is not today.';
                NotificationGuid: Label 'c75d47de-0d9f-47af-a16b-1ac331f6bce5';
            begin
                If Rec."Posting Date" <> Today() then begin
                    
                    MyNotification.Id(NotificationGuid);
                    If MyNotification.Recall() then;
                    MyNotification.Message(PostingDateTxt);
                    MyNotification.Scope(NotificationScope::LocalScope);
                    MyNotification.Send();
                end;
            end;
        }
    }
}

Easy. The new notification will replace the old one. Let’s try adding action into the notification.

Sample 3 : Create notification to inform user if the Posting Date is not today. However, remove old notification before sending a new one. Add action to update Posting Date to Today’s date.

To add an action is a bit trickier. You will need to create new codeunit, then use setdata and getdata to pass the parameter.

Below example shows how the Sales Order No. is passed by using SetData and GetData.

codeunit 50101 "Update Posting Date"
{
    procedure UpdatePostingDateToToday(var MyNotification: Notification)
    var
        SalesHeader: Record "Sales Header";
        SalesOrderNoLbl: Label 'SalesOrder';
        SalesOrderNo: Code[20];
    begin
        SalesOrderNo := MyNotification.GetData(SalesOrderNoLbl);
        SalesHeader.Get(SalesHeader."Document Type"::Order, SalesOrderNo);
        SalesHeader.Validate("Posting Date", Today());
        SalesHeader.Modify(true);
    end;
}
tableextension 50100 "My Sales Header" extends "Sales Header"
{
    fields
    {
        modify("Posting Date")
        {
            trigger OnAfterValidate()
            var
                MyNotification: Notification;
                PostingDateTxt: Label 'Posting Date is not today.';
                NotificationGuid: Label 'c75d47de-0d9f-47af-a16b-1ac331f6bce5';
                UpdatePostingDateQst: Label 'Update Posting Date ?';
                SalesOrderNoLbl: Label 'SalesOrder';
            begin
                If Rec."Posting Date" <> Today() then begin
                    MyNotification.Id(NotificationGuid);
                    If MyNotification.Recall() then;
                    MyNotification.Message(PostingDateTxt);
                    MyNotification.Scope(NotificationScope::LocalScope);
                    MyNotification.SetData(SalesOrderNoLbl, Rec."No.");
                    MyNotification.AddAction(UpdatePostingDateQst, Codeunit::"Update Posting Date", 'UpdatePostingDateToToday');
                    MyNotification.Send();
                end;
            end;
        }
    }
}

When we update the posting date now, we get action on the notification.

If you click on “Update Posting Date?”, the codeunit will run and update the Posting Date to Today’s date.

Nice. Hope that explains how to use Notification on your project.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. 24 February 2022

    […] Create Notifications in BC […]

Leave a Reply

Your email address will not be published. Required fields are marked *