al-codeactions icon indicating copy to clipboard operation
al-codeactions copied to clipboard

Refactor Message to Notification

Open StefanMaron opened this issue 2 years ago • 11 comments

Hi David!

I just realized that I could make far more use of Notifcations and that it often makes sense to use a notification instead of a message. That way the flow in BC can better for the user, as he does not get interrupted by message so often.

What do you think about the idea to refactor A message into a notification.

This:

Message('Some message to show to the user');

To this:

MyNotification.Message('Some message to show to the user');
MyNotification.Send();

When a label already is in place, that should be used obviously.

StefanMaron avatar Oct 14 '22 06:10 StefanMaron

May be also add a unique, hardcoded notificationid when you're at it ;-)

waldo1001 avatar Oct 14 '22 12:10 waldo1001

May be also add a unique, hardcoded notificationid when you're at it ;-)

Is there any benefit to have an ID there?

StefanMaron avatar Oct 14 '22 13:10 StefanMaron

Hm, may be not in this case. An ID will be assigned anyway (in memory). Only interesting to be recalled (or to make sure the same is not being added multiple times)

waldo1001 avatar Oct 14 '22 13:10 waldo1001

An ID could also be good if you want to turn off the notification, but then it would become a bit more complex. Probably too much, right? Or would you like to have at least the possibility to have the code afterwards like this?

var
    TheMessageTxt: Label 'Some message to show to the user';

procedure ExistingProc()
begin
    // [some other existing code]
    CallNotification();
    // [some other existing code]
end;

local procedure CallNotification()
var
    MyNotifications: Record "My Notifications";
begin
    if MyNotifications.IsEnabled(GetNotificationId()) then begin
        MyNotification.Id := GetNotificationId();
        MyNotification.Message(TheMessageTxt);
        MyNotification.Send();
    end;
end;

local procedure GetNotificationId(): Guid
begin
    exit('5dd5a7df-14cc-49b6-9d16-b34066c06f05');
end;

[EventSubscriber(ObjectType::Page, Page::"My Notifications", 'OnInitializingNotificationWithDefaultState', '', false, false)]
local procedure OnInitializingNotificationWithDefaultState()
var
    MyNotifications: Record "My Notifications";
    DescriptionTxt: Label 'Text inserted by snippet';
begin
    MyNotifications.InsertDefault(GetNotificationId(), TheMessageTxt, DescriptionTxt, true);
end;

DavidFeldhoff avatar Oct 14 '22 15:10 DavidFeldhoff

It could be two codeactions like:

  1. Convert to Notification
  2. Convert to optional Notification

DavidFeldhoff avatar Oct 14 '22 15:10 DavidFeldhoff

I like to have both posibilities, the full example and the very minimalistic one :)

StefanMaron avatar Oct 14 '22 15:10 StefanMaron

But at least the "Convert to optional notification" would only work in codeunits right? Because only there I'm able to create event subscribers..

DavidFeldhoff avatar Oct 14 '22 15:10 DavidFeldhoff

I mean, you could still create it so its there to be copied somewhere else, right?

StefanMaron avatar Oct 14 '22 15:10 StefanMaron

Yes, would be possible, but then "suddenly" after a codeaction your solution isn't compilable anymore right? I can imagine that a few people are then a bit confused. If I'd do it like that, I'd at least put a comment to it or something like that

DavidFeldhoff avatar Oct 14 '22 15:10 DavidFeldhoff

Or you could also only offer the complex scenario if the code already is withing a codeunit :)

StefanMaron avatar Oct 27 '22 03:10 StefanMaron

Short update: haven't started with this yet, as I'm a bit pushing the other two extensions currently (ATDD.TestScriptor and Azure DevOps Simplify). Will come to this at a later time

DavidFeldhoff avatar Dec 02 '22 05:12 DavidFeldhoff