bcquality/microsoft/knowledge/events/database-trigger-setup-flags-may-only-be-set-to-true.good.al
Michael Dieringer 4cd53eac64
knowledge(events): database trigger setup flags may only be set to true (#213)
* knowledge(events): database trigger setup flags may only be set to true

Add an events article with compiled good/bad samples:
GetDatabaseTableTriggerSetup (Global Triggers) and
OnAfterGetDatabaseTableTriggerSetup (GlobalTriggerManagement) share four
var Booleans across all subscribers, which run in no particular order.
Assigning false, or a lookup result without or-ing in the current value,
clears flags other features set (Dataverse sync, API webhooks, data
archive, and, for direct Global Triggers subscribers, the change log).
Recommends the codeunit 49 integration events per Learn's guidance on
system codeunits 2000000001..2000000010 and handler-side table filtering.

Wired into al-events-review tokens and an event-design check, with
carve-outs for conditional := true, Flag := Flag or ..., and the no-op
"if not Flag then Flag := false" found in BCApps. Registered in the
events review-fixtures override.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* knowledge(events): address review of database trigger setup flags article

- Name the BCApps code that clears flags (demo data tool W1/CZ/IN,
  Backup Management test library) as the mechanism in demo/test-only
  sessions; carve such code out of the al-events-review check.
- Label "events are raised only when the flag is true" as inferred and
  cite the supporting sources (Learn integration-record refactoring on
  disabled bulk SQL operations, ChangeLog test cache comment,
  No Transactions Subscriber); fold the bulk-SQL cost into Best Practice.
- Drop the "handler without opt-in" signal from article and check.
- Qualify change log protection as normal execution context only.
- Drop the unverifiable codeunit ID; describe Global Triggers by its
  2000000001..2000000010 range and mark the transition page as v14-era.
- Order-dependent wording for flag overwrites; add GP and
  No Transactions as further direct subscribers; link the references.

Samples rechecked with alc 30.0 against Base Application 28.4 symbols.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 14:59:53 +02:00

65 lines
1.9 KiB
AL

codeunit 50112 "Change Tracking Subscr. Good"
{
// Self-contained demonstration of the best practice. Not derived from base-app source.
[EventSubscriber(ObjectType::Codeunit, Codeunit::GlobalTriggerManagement, OnAfterGetDatabaseTableTriggerSetup, '', false, false)]
local procedure OptInTrackedTables(TableId: Integer; var OnDatabaseInsert: Boolean; var OnDatabaseModify: Boolean; var OnDatabaseDelete: Boolean; var OnDatabaseRename: Boolean)
var
TrackedTable: Record "Tracked Table Good";
begin
// Only ever turn flags on; flags set by other features stay untouched.
if not TrackedTable.Get(TableId) then
exit;
OnDatabaseModify := true;
OnDatabaseDelete := true;
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::GlobalTriggerManagement, OnAfterOnDatabaseModify, '', false, false)]
local procedure LogModify(RecRef: RecordRef)
var
TrackedTable: Record "Tracked Table Good";
TrackedChange: Record "Tracked Change Good";
begin
// The event also fires for tables other features opted in.
if RecRef.IsTemporary() then
exit;
if not TrackedTable.Get(RecRef.Number()) then
exit;
TrackedChange.Init();
TrackedChange."Table No." := RecRef.Number();
TrackedChange."Record ID" := RecRef.RecordId();
TrackedChange.Insert();
end;
}
table 50112 "Tracked Table Good"
{
DataClassification = SystemMetadata;
fields
{
field(1; "Table No."; Integer) { }
}
keys
{
key(PK; "Table No.") { Clustered = true; }
}
}
table 50113 "Tracked Change Good"
{
DataClassification = SystemMetadata;
fields
{
field(1; "Entry No."; Integer) { AutoIncrement = true; }
field(2; "Table No."; Integer) { }
field(3; "Record ID"; RecordId) { }
}
keys
{
key(PK; "Entry No.") { Clustered = true; }
}
}