{"title":"System.GetLastErrorCode() Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/system/system-getlasterrorcode-method/","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/methods-auto/system/system-getlasterrorcode-method, 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/methods-auto/system/system-getlasterrorcode-method","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":"Gets the classification of the last error that occurred.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":117,"chars":199},{"level":2,"title":"Return Value","slug":"return-value","at":316,"chars":52},{"level":2,"title":"Remarks","slug":"remarks","at":368,"chars":357},{"level":2,"title":"Example","slug":"example","at":725,"chars":698},{"level":2,"title":"Related information","slug":"related-information","at":1423,"chars":134}],"text":"> Version: _Available or changed with runtime version 1.0._\nGets the classification of the last error that occurred.\n## Syntax\n```al\nString :=   System.GetLastErrorCode()\n```\n> NOTE\n> This method can be invoked using property access syntax.\n> NOTE\n> This method can be invoked without specifying the data type name.\n## Return Value\nString\nType: ../text/text-data-type\n## Remarks\nYou use the GetLastErrorCode method to identify the type of the last error that occurred. You use the system-getlasterrortext--method to get the details of the last error.\nThe result of the GetLastErrorCode method is not translated into the local language. The result of the system-getlasterrortext--method is translated into the local language.\n## Example\nThis example opens a file and then causes an error to occur by trying to open the file again. If the code for the error that occurred is not the expected error code, then an error message is displayed.\n```al\nvar\n    ErrorCode: Text[1024];\n    ExpectedErrorCode: Text[1024];\n    FileObj1: File;\n    FileObj2: File;\n    Text0001: Label 'The error code is not valid. Expected: %1. Actual: %2. Error message: %3';\nbegin\n    ExpectedErrorCode := 'StreamIO';\n    FileObj1.CreateTempFile;\n    AssertError FileObj2.Create(FileObj1.Name);\n    ErrorCode := GetLastErrorCode;\n    if ErrorCode <> ExpectedErrorCode then\n      Error(Text001, ExpectedErrorCode, ErrorCode, GetLastErrorText);\nend;\n```\n## Related information\nsystem-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}