al-codeactions
al-codeactions copied to clipboard
Refactor Message to Notification
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.
May be also add a unique, hardcoded notificationid when you're at it ;-)
May be also add a unique, hardcoded notificationid when you're at it ;-)
Is there any benefit to have an ID there?
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)
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;
It could be two codeactions like:
- Convert to Notification
- Convert to optional Notification
I like to have both posibilities, the full example and the very minimalistic one :)
But at least the "Convert to optional notification" would only work in codeunits right? Because only there I'm able to create event subscribers..
I mean, you could still create it so its there to be copied somewhere else, right?
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
Or you could also only offer the complex scenario if the code already is withing a codeunit :)
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