{"title":"OnRun (Codeunit) trigger","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/triggers-auto/codeunit/devenv-onrun-codeunit-trigger/","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/triggers-auto/codeunit/devenv-onrun-codeunit-trigger, 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/triggers-auto/codeunit/devenv-onrun-codeunit-trigger","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":"Runs when a codeunit is run.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":89,"chars":55},{"level":2,"title":"Example","slug":"example","at":144,"chars":525},{"level":2,"title":"Related information","slug":"related-information","at":669,"chars":117}],"text":"> Version: _Available or changed with runtime version 1.0._\nRuns when a codeunit is run.\n## Syntax\n```al\ntrigger OnRun()\nbegin\n    ...\nend;\n```\n## Example\nThe `OnRun()` trigger is used to initialize the codeunit. A codeunit can be called from another codeunit, a page, or a report. The `OnRun()` trigger is run when the codeunit is called. The following pseudo code illustrates how the `OnRun()` trigger is run when a codeunit is called from a page.\n```al\npage 50111 MyPage\n{\n    trigger OnOpenPage();\n    begin\n        Codeunit.Run(Codeunit::RunMyCode);\n    end;\n}\n\ncodeunit 50110 RunMyCode\n{\n    trigger OnRun()\n    begin\n        // Run some code here\n    end;\n}\n```\n## Related information\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}