bcquality/microsoft/knowledge/privacy/ship-a-default-retention-policy-setup.good.al
Jeremy Vyska 2f3e5afac1 Add retention policy knowledge to the privacy domain
Two articles covering retention policies for extension-owned tables,
the gap that lets high-volume log tables grow unbounded:

- register-owned-log-tables-for-retention-policies: an extension's own
  log tables must be added to the allowed-tables list from install AND
  upgrade code, guarded by IsAllowedTable plus an upgrade tag, with a
  mandatory minimum retention where audit needs one.
- ship-a-default-retention-policy-setup: registration only makes a table
  selectable; nothing is deleted until a Retention Policy Setup record
  exists, so ship one (disabled by default) as the platform's own
  Retention Policy Installer does.

Each ships good/bad AL samples. Claims verified against the BC admin
docs and the Retention Policy module in microsoft/BCApps.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011gvTjm746MtJEVWeRTbG46
2026-09-10 12:58:26 +02:00

48 lines
1.8 KiB
AL

codeunit 50564 "Contoso Reten. Pol. Default"
{
Access = Internal;
var
SixMonthsTok: Label 'Six Months', MaxLength = 20;
procedure CreateDefaultPolicy()
var
RetentionPolicySetup: Record "Retention Policy Setup";
UpgradeTag: Codeunit "Upgrade Tag";
begin
// Created once per company: an administrator who deletes the policy
// does not get it back on the next upgrade.
if UpgradeTag.HasUpgradeTag(DefaultPolicyTag()) then
exit;
if not RetentionPolicySetup.Get(Database::"Contoso Activity Log") then begin
RetentionPolicySetup.Validate("Table Id", Database::"Contoso Activity Log");
RetentionPolicySetup.Validate("Apply to all records", true);
RetentionPolicySetup.Validate("Retention Period", SixMonthRetentionPeriod());
RetentionPolicySetup.Validate(Enabled, false); // the administrator opts in to deletion
RetentionPolicySetup.Insert(true);
end;
UpgradeTag.SetUpgradeTag(DefaultPolicyTag());
end;
local procedure SixMonthRetentionPeriod(): Code[20]
var
RetentionPeriod: Record "Retention Period";
begin
RetentionPeriod.SetRange("Retention Period", RetentionPeriod."Retention Period"::"6 Months");
if RetentionPeriod.FindFirst() then
exit(RetentionPeriod.Code);
RetentionPeriod.Code := CopyStr(UpperCase(SixMonthsTok), 1, MaxStrLen(RetentionPeriod.Code));
RetentionPeriod.Description := SixMonthsTok;
RetentionPeriod.Validate("Retention Period", RetentionPeriod."Retention Period"::"6 Months");
RetentionPeriod.Insert(true);
exit(RetentionPeriod.Code);
end;
local procedure DefaultPolicyTag(): Code[250]
begin
exit('Contoso-ActivityLogDefaultPolicy-20260910');
end;
}