{"title":"Codeunit object","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-codeunit-object/","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-codeunit-object, 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-codeunit-object","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":"Describes the codeunit object, which is a container for business logic in AL for Business Central.","date":"2025-05-19","meta":{"topic":"how-to"},"outline":[{"level":2,"title":"Snippet support","slug":"snippet-support","at":217,"chars":151},{"level":2,"title":"Codeunit example","slug":"codeunit-example","at":368,"chars":700},{"level":2,"title":"Codeunit self-reference keyword","slug":"codeunit-self-reference-keyword","at":1068,"chars":433},{"level":2,"title":"Related information","slug":"related-information","at":1501,"chars":348}],"text":"A codeunit is a container for AL code that you can use in many application objects. You typically implement business logic in codeunits and call the codeunit from the object that needs to perform that specific logic.\n## Snippet support\nTyping the shortcut tcodeunit creates the basic layout for a codeunit object when using the Business Central in Visual Studio Code.\n## Codeunit example\nThis codeunit example checks whether a given customer registered a shoe size. If not, the customer is assigned a shoe size of 42. The example is for illustrational purposes only; the customer table doesn't have a ShoeSize field by default.\nThe codeunit can be used both as a direct call to `codeunit.run(customer)` or as a call to the procedure inside the codeunit `createcustomer.CheckSize(customer)`.\n```al\ncodeunit 50113 CreateCustomer\n{\n    TableNo = Customer;\n\n    trigger OnRun();\n    begin\n        CheckSize(Rec);\n    end;\n\n    procedure CheckSize(var Cust : Record Customer)\n    begin\n        if not Cust.HasShoeSize() then\n            Cust.ShoeSize := 42;\n    end;\n}\n```\n## Codeunit self-reference keyword\nThe this keyword can be used in codeunits in AL as a self-reference, and it allows passing the current object as an argument to methods. Additionally, using the this keyword enhances readability in larger methods by clearly indicating whether a variable is in the global or local scope. Learn more about the this keyword in Use the this keyword for codeunit self-reference<devenv-al-this-keyword>.\n## Related information\nDeveloping extensions<devenv-dev-overview>\nTable extension object<devenv-table-ext-object>\nPage extension object<devenv-page-ext-object>\nAL development environment<devenv-reference-overview>\nDocumenting your code with XML comments<devenv-xml-comments>\nUse the this keyword for codeunit self-reference<devenv-al-this-keyword>."}