Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Version: Available or changed with runtime version 6.0.
Specifies the behavior of a commit call inside the method scope.
Applies to
- Method
Syntax
[CommitBehavior(Behavior: CommitBehavior)]
Arguments
Behavior
Type: CommitBehavior
Specifies if a commit must be ignored or throw an error. The options are: Ignored or Error.
Remarks
It's only possible to assign a more restrictive commit behavior. That is, if
CommitBehavior::Ignoreis attempted on a method scope, but the method calling the current method, for example, the parent method is actually running withCommitBehavior::Error, then the current method will continue running withCommitBehavior::Error, even though theIgnoreattribute was specified.The
CommitBehavioronly lasts for the method scope. Regardless of whether the method finishes successfully or an error causes the method to exit prematurely, theCommitBehaviorreturns to the behavior that was in effect before the method was called.The
CommitBehavioronly applies to explicit commits, not implicit commits done as part of Codeunit.Run.
Local method example
The example shown below illustrates how the attribute is used on a local method; it can also be applied on a global method.
codeunit 50100 MyCodeunit
{
trigger OnRun()
begin
FunctionAllowCommit();
end;
local procedure FunctionAllowCommit()
begin
FunctionIgnoreCommit();
Commit(); // This commit is executed.
end;
[CommitBehavior(CommitBehavior::Ignore)]
local procedure FunctionIgnoreCommit()
begin
TryFunctionErrorCommit();
Commit(); // This commit is ignored.
end;
[CommitBehavior(CommitBehavior::Error)]
[TryFunction]
local procedure TryFunctionErrorCommit()
begin
Commit(); // This commit causes an error.
end;
}
Event subscriber example
This example illustrates how you can protect your code from commits happening in event subscriber code; typically written by a third party.
codeunit 50102 MyPublishingCodeunit
{
// Ignore explicit commits attempted by event subscribers.
[CommitBehavior(CommitBehavior::Ignore)]
[IntegrationEvent(true, false)]
procedure OnSomethingChangedEvent()
begin
// This part of the operation is extensible.
end;
procedure Validate(): Boolean
begin
exit(true);
end;
procedure DoImportantAtomicOperation()
begin
// Do work before notifying subscribers.
OnSomethingChangedEvent();
// Do more work after notifying subscribers.
if Validate() then
Commit()
else
Error('Validation failed');
end;
}
codeunit 50103 MySubscribingCodeunit
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::MyPublishingCodeunit, 'OnSomethingChangedEvent', '', true, true)]
local procedure SubscribeToOnSomethingChangedEvent(Sender: Codeunit MyPublishingCodeunit)
begin
Commit();
end;
}