Hi, Readers.
Dynamics 365 Business Central supports different types of events for different purposes, Business events, Integration events, Global events, and Trigger events.
Today I would like to briefly talk about “Global Triggers“. “Global Triggers” is one of Global events. Global events are predefined system events that are automatically raised by various base application codeunits. For example, ‘OnDatabaseInsert‘, ‘OnDatabaseModify‘, etc.
This is very useful for API / Odata and Changelog. When the table data changes (insert, modify, delete, rename), these triggers can be executed.
Last week I was asked a question about why the Global Trigger added in the extension wasn’t working. I checked his code.
For example:
There is no problem with the code, but for some performance reasons, Global Triggers are only worked for tables and triggers in the ‘GetDatabaseTableTriggerSetup‘ event registered. So you need to register your tables and triggers in the ‘GetDatabaseTableTriggerSetup‘ event first.
You can find an example in “codeunit 49 GlobalTriggerManagement“
For Changelog:
Let’s add a new event subscriber and register Payment Terms table.
Source Code:
codeunit 50101 GlobalEventHandle
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Global Triggers", 'GetDatabaseTableTriggerSetup', '', false, false)]
local procedure GetDatabaseTableTriggerSetup(TableId: Integer; var OnDatabaseInsert: Boolean; var OnDatabaseModify: Boolean; var OnDatabaseDelete: Boolean)
begin
if TableID = Database::"Payment Terms" then begin
OnDatabaseInsert := true;
OnDatabaseModify := true;
OnDatabaseDelete := true;
end;
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Global Triggers", 'OnDatabaseInsert', '', false, false)]
local procedure OnDatabaseInsert()
begin
Message('Global Trigger Event OnDatabaseInsert');
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Global Triggers", 'OnDatabaseModify', '', false, false)]
local procedure OnDatabaseModify()
begin
Message('Global Trigger Event OnDatabaseModify');
end;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Global Triggers", 'OnDatabaseDelete', '', false, false)]
local procedure OnDatabaseDelete()
begin
Message('Global Trigger Event OnDatabaseDelete');
end;
}
Test Video:
Note: If you do not specify any table in the ‘GetDatabaseTableTriggerSetup‘ event, the Global Triggers will take effect for all tables. So please remember, use Global Triggers only when you really need it, and you must specify the table when you use it.
PS: codeunit 2000000002 “Global Triggers” (BC19.0)
codeunit 2000000002 "Global Triggers"
{
trigger OnRun()
begin
end;
[BusinessEvent(false)]
procedure GetGlobalTableTriggerMask(TableID: Integer; var TableTriggerMask: Integer)
begin
end;
[BusinessEvent(false)]
procedure OnGlobalInsert(RecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnGlobalModify(RecRef: RecordRef; xRecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnGlobalDelete(RecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnGlobalRename(RecRef: RecordRef; xRecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure GetDatabaseTableTriggerSetup(TableId: Integer; var OnDatabaseInsert: Boolean; var OnDatabaseModify: Boolean; var OnDatabaseDelete: Boolean; var OnDatabaseRename: Boolean)
begin
end;
[BusinessEvent(false)]
procedure OnDatabaseInsert(RecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnDatabaseModify(RecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnDatabaseDelete(RecRef: RecordRef)
begin
end;
[BusinessEvent(false)]
procedure OnDatabaseRename(RecRef: RecordRef; xRecRef: RecordRef)
begin
end;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント