Variable Persistence Pattern #61
waldo1001
started this conversation in
BC Patterns
Replies: 1 comment
-
while I understand that this patterns sometimes may be the only way to achieve a goal, I would not feel good to implement it. I think if this is used, there should be a dedicated codeunit for each "task". And there should be comments explaining what is going on. Something like this: codeunit 50000 "MyAwesomeMethod Sub"
{
SingleInstance = true;
// This codeunit is dedicated to do [a specific task]
// to achieve this, a number of values need to be saved as global variables
// and the codeunit is set to single instance to save the values until all the events are called
// DO NOT ADD ANY EVENT SUBSCRIBER THAT IS NOT DEDICATED TO THIS TASK
var
IsPreviewMode: Boolean;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostLines', '', false, false)]
local procedure SavePreivewMode_OnBeforePostLines(PreviewMode: Boolean)
begin
IsPreviewMode := PreviewMode;
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesLines', '', false, false)]
local procedure DoSomething_OnAfterPostSalesLines(var SalesHeader: Record "Sales Header")
begin
if not IsPreviewMode then
// Do something
clear(IsPreviewMode);
end;
} What also should be addressed I think: What if, for some reason, the second event is not called and the variable(s) are not cleared? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Not all events contain the needed parameters to be able to execute what you need at that certain event. But another event prior to that, might container the necessary parameters.
This pattern is one that persists the variable of the first event (by means of a single instance codeunit) to the second event.
Beta Was this translation helpful? Give feedback.
All reactions