{"title":"Collecting errors","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-error-collection/","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/devenv-error-collection, 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/devenv-error-collection","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":"Learn how to write AL code that returns more than one error and presents users with more detailed error information.","date":"2025-05-19","meta":{"topic":"concept-article"},"outline":[{"level":1,"title":"Collecting errors","slug":"collecting-errors","at":0,"chars":6168},{"level":2,"title":"How to use the collectible errors feature","slug":"how-to-use-the-collectible-errors-feature","at":303,"chars":1015},{"level":2,"title":"Collectible errors API","slug":"collectible-errors-api","at":1318,"chars":233},{"level":2,"title":"Example","slug":"example","at":1551,"chars":4336},{"level":2,"title":"Related information","slug":"related-information","at":5887,"chars":281}],"text":"# Collecting errors\nThis article explains how to write AL code that captures multiple errors and displays them in the user interface. This feature can simplify validation scenarios and is referred to as collectible errors. Specifically, scenarios where users are presented with a list of things to fix.\n## How to use the collectible errors feature\nNormally, when an error occurs in a procedure, the procedure stops on the first error it meets. Using collectable errors essentially postpones error handling to the end of the procedure call. AL code execution doesn't stop on errors. But instead, it continues until the end and gathers errors as they occur.\nOne use of the collectible errors feature is for validating multiple conditions, where you check each condition with a collectible error. Then, instead of stopping the user and presenting each validation separately, you present all errors collected in a single page during the final validation.\n> IMPORTANT\n> It's recommended that you handle all collectible errors from your AL code. Otherwise, if any errors are present in the collected list when a procedure ends, the user receives an error dialog that concatenates all the error messages. This error dialog can be difficult for users to understand. See the example below for how to handle collectible errors.\n## Collectible errors API\nAL includes several methods, properties, and attributes that are designed specifically for the collectable errors feature. For an overview of the API, see Collecting Errors API<devenv-error-collection-api>.\n## Example\nThe following code example illustrates how to use collectible errors. It's built around the DoPost codeunit, which sets simple criteria on what can or can't be included in table fields. By itself, this procedure stops when errors occur. By applying the `ErrorBehavior(ErrorBehavior::Collect)` attribute, the `PostWithErrorCollect ()` and `PostWithErrorCollectCustomUI ()` procedures show you a couple ways to collect and present these errors.\n```al\npageextension 50100 CollectingErrorsExt extends \"Customer List\"\n{\n    actions\n    {\n        addfirst(processing)\n        {\n            // This action doesn't collect errors. Any procedure will stop on the first error that occurs,\n            // and return the error.\n            action(Post)\n            {\n                ApplicationArea = All;\n                trigger OnAction()\n                var\n                    i: Record Integer;\n                begin\n                    i.Number := -9;\n                    Codeunit.Run(Codeunit::DoPost, i);\n                end;\n            }\n\n            // This action collects errors. The PostWithErrorCollect procedure continues on errors,\n            // and displays the errors in a dialog to the user done.\n            action(PostWithErrorCollect)\n            {\n                ApplicationArea = All;\n                trigger OnAction()\n                begin\n                    PostWithErrorCollect();\n                end;\n            }\n\n            // This action collects errors. The PostWithErrorCollectCustomUI procedure continues on errors,\n            // and displays error details in a list page when done.\n            // This implementation illustrates how you could design your own UI for displaying and\n            // troubleshooting errors.\n            action(PostWithErrorCollectCustomUI)\n            {\n                ApplicationArea = All;\n                trigger OnAction()\n                begin\n                    PostWithErrorCollectCustomUI();\n                end;\n            }\n        }\n    }\n\n    [ErrorBehavior(ErrorBehavior::Collect)]\n    procedure PostWithErrorCollect()\n    var\n        i: Record Integer;\n    begin\n        i.Number := -9;\n        Codeunit.Run(Codeunit::DoPost, i);\n        // After executing the codeunit, there will be collected errors,\n        // and therefore an error dialog will be shown when exiting this procedure.\n    end;\n\n    [ErrorBehavior(ErrorBehavior::Collect)]\n    procedure PostWithErrorCollectCustomUI()\n    var\n        errors: Record \"Error Message\" temporary;\n        error: ErrorInfo;\n        i: Record Integer;\n    begin\n        i.Number := -9;\n        // By using Codeunit.Run, you ensure any changes to the database within\n        // Codeunit::DoPost are rolled back in case of errors.\n        if not Codeunit.Run(Codeunit::DoPost, i) then begin\n            // If Codeunit.Run fails, a non-collectible error was encountered,\n            // add this to the list of errors.\n            errors.ID := errors.ID + 1;\n            errors.Message := GetLastErrorText();\n            errors.Insert();\n        end;\n\n        // If there are collected errors, iterate through each of them and\n        // add them to \"Error Message\" record.\n        if HasCollectedErrors then\n            foreach error in system.GetCollectedErrors() do begin\n                errors.ID := errors.ID + 1;\n                errors.Message := error.Message;\n                errors.Validate(\"Record ID\", error.RecordId);\n                errors.Insert();\n            end;\n\n        // Clearing the collected errors will ensure the built-in error dialog\n        // will not show, but instead show our own custom \"Error Messages\" page.\n        ClearCollectedErrors();\n\n        page.RunModal(page::\"Error Messages\", errors);\n    end;\n}\n\ncodeunit 50100 DoPost\n{\n    TableNo = Integer;\n\n    trigger OnRun()\n    begin\n        if Rec.Number mod 2 <> 0 then\n            Error(ErrorInfo.Create('Number should be equal', true, Rec, Rec.FieldNo(Number)));\n\n        if Rec.Number <= 0 then\n            Error(ErrorInfo.Create('Number should be larger than 0', true, Rec, Rec.FieldNo(Number)));\n\n        if Rec.Number mod 3 = 0 then\n            Error(ErrorInfo.Create('Number should not be divisible by 10', true, Rec, Rec.FieldNo(Number)));\n\n        // Everything was valid, do the actual posting.\n    end;\n}\n```\n## Related information\nFailure modeling and robust coding practices<devenv-robust-coding-practices>\ndevenv-al-error-handling\ndevenv-al-simple-statements\nCollecting Errors API<devenv-error-collection-api>\nError handling overview<devenv-al-error-handling>\ndevenv-al-simple-statements"}