Skip to content

Commit

Permalink
[Email] Add retention policy for email inbox (#1887)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
Add retention policy for Email Inbox. The length being 2 days retention.

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#539754](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/539754)
  • Loading branch information
darjoo authored Aug 27, 2024
1 parent 7afc36b commit 7c59b85
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
60 changes: 59 additions & 1 deletion src/System Application/App/Email/src/EmailInstaller.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ codeunit 1596 "Email Installer"
procedure AddRetentionPolicyAllowedTables()
begin
AddRetentionPolicyAllowedTables(false);
AddRetentionPolicyEmailInboxAllowedTable(false);
CreateRetentionPolicySetup(false);
end;

procedure AddRetentionPolicyAllowedTables(ForceUpdate: Boolean)
Expand All @@ -46,21 +48,77 @@ codeunit 1596 "Email Installer"

RetenPolAllowedTables.AddAllowedTable(Database::"Email Outbox", Field.FieldNo(SystemCreatedAt), 7);
RetenPolAllowedTables.AddAllowedTable(Database::"Sent Email", Field.FieldNo(SystemCreatedAt), 7);
RetenPolAllowedTables.AddAllowedTable(Database::"Email Inbox", Field.FieldNo(SystemCreatedAt), 7);

if IsInitialSetup then
UpgradeTag.SetUpgradeTag(GetEmailTablesAddedToAllowedListUpgradeTag());
end;

procedure AddRetentionPolicyEmailInboxAllowedTable(ForceUpdate: Boolean)
var
Field: Record Field;
RetenPolAllowedTables: Codeunit "Reten. Pol. Allowed Tables";
UpgradeTag: Codeunit "Upgrade Tag";
IsInitialSetup: Boolean;
begin
IsInitialSetup := not UpgradeTag.HasUpgradeTag(GetEmailInboxAddedToAllowedListUpgradeTag());
if not (IsInitialSetup or ForceUpdate) then
exit;

RetenPolAllowedTables.AddAllowedTable(Database::"Email Inbox", Field.FieldNo(SystemCreatedAt), 2);

if IsInitialSetup then
UpgradeTag.SetUpgradeTag(GetEmailInboxAddedToAllowedListUpgradeTag());
end;

procedure CreateRetentionPolicySetup(ForceUpdate: Boolean)
var
RetentionPolicySetup: Record "Retention Policy Setup";
RetentionPolicySetupCU: Codeunit "Retention Policy Setup";
UpgradeTag: Codeunit "Upgrade Tag";
DateFormula: DateFormula;
IsInitialSetup: Boolean;
begin
IsInitialSetup := not UpgradeTag.HasUpgradeTag(GetEmailInboxPolicyAddedToAllowedListUpgradeTag());
if not (IsInitialSetup or ForceUpdate) then
exit;

RetentionPolicySetup.SetRange("Table Id", Database::"Email Inbox");
if not RetentionPolicySetup.IsEmpty() then
exit;

Evaluate(DateFormula, '<-2D>');

RetentionPolicySetup.Validate("Table Id", Database::"Email Inbox");
RetentionPolicySetup.Validate("Apply to all records", true);
RetentionPolicySetup.Validate("Retention Period", RetentionPolicySetupCU.FindOrCreateRetentionPeriod('2 DAYS', "Retention Period Enum"::"Custom", DateFormula));
RetentionPolicySetup.Validate(Enabled, true);
RetentionPolicySetup.Insert(true);

if IsInitialSetup then
UpgradeTag.SetUpgradeTag(GetEmailInboxPolicyAddedToAllowedListUpgradeTag());
end;

local procedure GetEmailTablesAddedToAllowedListUpgradeTag(): Code[250]
begin
exit('MS-373161-EmailLogEntryAdded-20201005');
end;

local procedure GetEmailInboxAddedToAllowedListUpgradeTag(): Code[250]
begin
exit('MS-539754-EmailInboxAdded-20240827');
end;

local procedure GetEmailInboxPolicyAddedToAllowedListUpgradeTag(): Code[250]
begin
exit('MS-539754-EmailInboxPolicyAdded-20240827');
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Reten. Pol. Allowed Tables", OnRefreshAllowedTables, '', false, false)]
local procedure AddAllowedTablesOnRefreshAllowedTables()
begin
AddRetentionPolicyAllowedTables(true);
AddRetentionPolicyEmailInboxAllowedTable(true);
CreateRetentionPolicySetup(true);
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"System Initialization", OnAfterLogin, '', false, false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ codeunit 3902 "Retention Policy Setup"
exit(RetentionPolicySetupImpl.FindOrCreateRetentionPeriod(RetentionPeriodEnum, RetPeriodCalc))
end;

/// <summary>
/// Finds or creates a new retention period record for the provided retention period.
/// </summary>
/// <param name="RetentionPeriodEnum">Specifies the retention period length enum value for which the retention period record should be found or created.</param>
/// <param name="RetPeriodCalc">Specifies the retention period length date formula for which the retention period record should be found or created.</param>
/// <returns>The retention period code for the retention period record.</returns>
procedure FindOrCreateRetentionPeriod(RetentionPeriodCode: Code[20]; RetentionPeriodEnum: Enum "Retention Period Enum"; RetPeriodCalc: DateFormula): Code[20]
var
RetentionPolicySetupImpl: Codeunit "Retention Policy Setup Impl.";
begin
exit(RetentionPolicySetupImpl.FindOrCreateRetentionPeriod(RetentionPeriodCode, RetentionPeriodEnum, RetPeriodCalc))
end;

// these event subscribers are here because the Impl. codeunit has a manual subscriber

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Guided Experience", OnRegisterManualSetup, '', true, true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,20 @@ codeunit 3903 "Retention Policy Setup Impl."
end;
end;

procedure FindOrCreateRetentionPeriod(RetentionPeriodEnum: Enum "Retention Period Enum"): Code[20]
var
RetPeriodCalc: DateFormula;
begin
Evaluate(RetPeriodCalc, '');
exit(FindOrCreateRetentionPeriod(RetentionPeriodEnum, RetPeriodCalc))
end;

procedure FindOrCreateRetentionPeriod(RetentionPeriodEnum: Enum "Retention Period Enum"; RetPeriodCalc: DateFormula): Code[20]
begin
exit(FindOrCreateRetentionPeriod('', RetentionPeriodEnum, RetPeriodCalc))
end;

procedure FindOrCreateRetentionPeriod(CodeName: Code[20]; RetentionPeriodEnum: Enum "Retention Period Enum"; RetPeriodCalc: DateFormula): Code[20]
var
RetentionPeriod: Record "Retention Period";
begin
Expand All @@ -367,7 +380,10 @@ codeunit 3903 "Retention Policy Setup Impl."
exit(RetentionPeriod.Code);

// create
RetentionPeriod.Code := CreateUniqueRetentionPeriodCode(RetentionPeriodEnum);
if CodeName = '' then
RetentionPeriod.Code := CreateUniqueRetentionPeriodCode(RetentionPeriodEnum)
else
RetentionPeriod.Code := CodeName;
RetentionPeriod.Description := CopyStr(Format(RetentionPeriodEnum), 1, MaxStrLen(RetentionPeriod.Description));
RetentionPeriod.Validate("Retention Period", RetentionPeriodEnum);
if RetentionPeriod."Retention Period" = RetentionPeriod."Retention Period"::Custom then
Expand All @@ -376,14 +392,6 @@ codeunit 3903 "Retention Policy Setup Impl."
exit(RetentionPeriod.Code);
end;

procedure FindOrCreateRetentionPeriod(RetentionPeriodEnum: Enum "Retention Period Enum"): Code[20]
var
RetPeriodCalc: DateFormula;
begin
Evaluate(RetPeriodCalc, '');
exit(FindOrCreateRetentionPeriod(RetentionPeriodEnum, RetPeriodCalc))
end;

local procedure CreateUniqueRetentionPeriodCode(RetentionPeriodEnum: Enum "Retention Period Enum") RetentionPeriodCode: Code[20]
var
RetentionPeriod: Record "Retention Period";
Expand Down

0 comments on commit 7c59b85

Please sign in to comment.