{"title":"CommitBehavior attribute","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/attributes/devenv-commitbehavior-attribute/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/attributes/devenv-commitbehavior-attribute, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/attributes/devenv-commitbehavior-attribute","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Specifies the behavior of a commit call inside the method scope.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Applies to","slug":"applies-to","at":125,"chars":22},{"level":2,"title":"Syntax","slug":"syntax","at":147,"chars":237},{"level":3,"title":"Arguments","slug":"arguments","at":210,"chars":174},{"level":2,"title":"Remarks","slug":"remarks","at":384,"chars":782},{"level":2,"title":"Local method example","slug":"local-method-example","at":1166,"chars":762},{"level":2,"title":"Event subscriber example","slug":"event-subscriber-example","at":1928,"chars":1143},{"level":2,"title":"Related information","slug":"related-information","at":3071,"chars":111}],"text":"> Version: _Available or changed with runtime version 6.0._\nSpecifies the behavior of a commit call inside the method scope.\n## Applies to\n Method\n## Syntax\n```al\n[CommitBehavior(Behavior: CommitBehavior)]\n```\n### Arguments\nBehavior\nType: ../methods-auto/commitbehavior/commitbehavior-option\nSpecifies if a commit must be ignored or throw an error. The options are: Ignored or Error.\n## Remarks\n It's only possible to assign a more restrictive commit behavior. That is, if `CommitBehavior::Ignore` is attempted on a method scope, but the method calling the current method, for example, the parent method is actually running with `CommitBehavior::Error`, then the current method will continue running with `CommitBehavior::Error`, even though the Ignore attribute was specified.\n The CommitBehavior only lasts for the method scope. Regardless of whether the method finishes successfully or an error causes the method to exit prematurely, the CommitBehavior returns to the behavior that was in effect before the method was called.\n The CommitBehavior only applies to explicit commits, not implicit commits done as part of ../methods-auto/codeunit/codeunit-run-method.\n## Local method example\nThe example shown below illustrates how the attribute is used on a local method; it can also be applied on a global method.\n```al\ncodeunit 50100 MyCodeunit\n{\n    trigger OnRun()\n    begin\n        FunctionAllowCommit();\n    end;\n\n    local procedure FunctionAllowCommit()\n    begin\n        FunctionIgnoreCommit();\n        Commit(); // This commit is executed.\n    end;\n\n    [CommitBehavior(CommitBehavior::Ignore)]\n    local procedure FunctionIgnoreCommit()\n    begin\n        TryFunctionErrorCommit();\n        Commit(); // This commit is ignored.\n    end;\n\n    [CommitBehavior(CommitBehavior::Error)]\n    [TryFunction]\n    local procedure TryFunctionErrorCommit()\n    begin\n        Commit(); // This commit causes an error.\n    end;\n}\n```\n## Event subscriber example\nThis example illustrates how you can protect your code from commits happening in event subscriber code; typically written by a third party.\n```al\ncodeunit 50102 MyPublishingCodeunit\n{\n    // Ignore explicit commits attempted by event subscribers.\n    [CommitBehavior(CommitBehavior::Ignore)]\n    [IntegrationEvent(true, false)]\n    procedure OnSomethingChangedEvent()\n    begin\n        // This part of the operation is extensible.\n    end;\n\n    procedure Validate(): Boolean\n    begin\n        exit(true);\n    end;\n\n    procedure DoImportantAtomicOperation()\n    begin\n        // Do work before notifying subscribers.\n        OnSomethingChangedEvent();\n        // Do more work after notifying subscribers.\n\n        if Validate() then\n            Commit()\n        else\n            Error('Validation failed');\n    end;\n}\n\ncodeunit 50103 MySubscribingCodeunit\n{\n    [EventSubscriber(ObjectType::Codeunit, Codeunit::MyPublishingCodeunit, 'OnSomethingChangedEvent', '', true, true)]\n    local procedure SubscribeToOnSomethingChangedEvent(Sender: Codeunit MyPublishingCodeunit)\n    begin\n        Commit();\n    end;\n}\n\n```\n## Related information\nGet Started with AL<../devenv-get-started>\nDeveloping Extensions<../devenv-dev-overview>"}