Using Notifications instead of Message
In the old NAV, when we had to notify something to the user, we used the Message or the Confirm instruction. Today these methods are considered outdated, in particular in the first case.
In the AL language we have a new Data Type called Notification, with which we can notify something to the user in the colored bar in the upper part of a page, without asking the user to press Ok on the message as was in the past.
Let’s take a look at a practical example on how to use this new way to manage notifications.
Let’s suppose we want to notify to the user when a selected customer on a sales order has more than one shipping address associated. The solution is very easy: we have just to program the OnAfterValidate trigger of the Sell-to Customer No. field in the Sales Header table by writing this code:
As we can see the code is really simple. The Notify variable is called two times: in the first instruction we set the message to show to the user by using the method Message, and in the second we just call the method Send that activates the notification on the page.
Notifications have also an interesting method called AddAction, with which you can manage some user actions inside the notification.
In the example above, suppose we want to manage a possible choice between different shipping addresses in the notification itself, so that the user can change the shipping address from a list and modify the original order, like below:
In order to manage the actions, we need to create a codeunit containing a function to be called by the AddAction method inside our trigger, like below
The AddAction method requires the message to be shown (‘Choose one’), the ID of the codeunit containing the function, and a string with the name of the function to be called.
If we need to pass some parameters to the function, we need to use the method SetData, containing a string identifying the parameter and the value to be passed. In the example above we passed two parameters, the customer number (used by the function to open the shipping addresses) and the order number to be updated with the shipping address code selected by the user. The function in the codeunit must have a Notification as a parameter
In order to read the parameters, we need to use the method GetData, who retrieves the values passed by the caller with the SetData method.
With these values we can open the shipping addresses page, let the user choose one, and update the order.
Compared to the past, this method is not only more elegant but is even more efficient, since it avoids the use of the Confirm instruction used previously that was cause of many problems due to the fact that it’s a modal and consequently blocking message.