{"title":"Use preprocessor directives in AL","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/directives/devenv-directives-in-al/","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/directives/devenv-directives-in-al, 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/directives/devenv-directives-in-al","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 use conditional, region, and pragma preprocessor directives and define symbols in AL for Microsoft Dynamics 365 Business Central extensions.","date":"2026-10-05","meta":{"topic":"concept-article"},"outline":[{"level":1,"title":"Use preprocessor directives in AL","slug":"use-preprocessor-directives-in-al","at":0,"chars":6363},{"level":2,"title":"Conditional directives","slug":"conditional-directives","at":1136,"chars":1069},{"level":3,"title":"Logical operators in conditional directives","slug":"logical-operators-in-conditional-directives","at":1914,"chars":291},{"level":2,"title":"Defining and using preprocessor symbols","slug":"defining-and-using-preprocessor-symbols","at":2205,"chars":1682},{"level":3,"title":"Defining symbols globally in app.json","slug":"defining-symbols-globally-in-appjson","at":2475,"chars":449},{"level":3,"title":"Defining symbols in code","slug":"defining-symbols-in-code","at":2924,"chars":430},{"level":3,"title":"Undefining symbols","slug":"undefining-symbols","at":3354,"chars":225},{"level":3,"title":"How symbols evaluate","slug":"how-symbols-evaluate","at":3579,"chars":308},{"level":2,"title":"Examples","slug":"examples","at":3887,"chars":1877},{"level":3,"title":"Example 1: Simple conditional compilation","slug":"example-1-simple-conditional-compilation","at":3899,"chars":200},{"level":3,"title":"Example 2: Using symbols from app.json","slug":"example-2-using-symbols-from-appjson","at":4099,"chars":366},{"level":3,"title":"Example 3: Multiple conditions with logical operators","slug":"example-3-multiple-conditions-with-logical-operators","at":4465,"chars":387},{"level":3,"title":"Example 4: Using custom build symbols","slug":"example-4-using-custom-build-symbols","at":4852,"chars":633},{"level":3,"title":"Example 5: Using not operator","slug":"example-5-using-not-operator","at":5485,"chars":279},{"level":2,"title":"Related information","slug":"related-information","at":5764,"chars":599}],"text":"# Use preprocessor directives in AL\n> APPLIES TO: Business Central 2020 release wave 2 and later\nUse AL preprocessor directives to compile code conditionally, suppress compiler warnings, and organize code into collapsible regions. AL supports the following groups of directives:\n #conditional-directives\n devenv-directive-region\n devenv-directive-pragma\nAny code can be made conditional, including table fields. A conditional directive evaluates a Boolean expression. The expression can contain defined or undefined symbols, the Boolean literals true and false, parentheses, and supported operators. A defined symbol evaluates to true, and an undefined symbol evaluates to false. Symbols defined in a source file have file scope. Symbols defined in the app.json file are available throughout the extension.\n> NOTE\n> Built-in symbols are currently not supported in AL. Symbols must be defined in a specific file or in the app.json file.\n> NOTE\n> Personalization and profile configuration ignore `#pragma`, `#region`, and `#endregion` directives. Unsupported conditional directives, such as `#if`, `#elif`, and `#define`, cause an error.\n## Conditional directives\nThe following conditional preprocessor directives are supported in AL.\nConditional preprocessor directive|Description\n`#if`|Starts a conditional clause. The `#endif` directive ends it. Includes the following code when its expression evaluates to true.\n`#else`|Includes its code when no preceding branch in the conditional block evaluates to true.\n`#elif`|Evaluates another expression when no earlier branch in the conditional block was selected. If the expression evaluates to true, its code is included until the next conditional directive.\n`#endif`|Ends the conditional clause that begins with `#if`.\n`#define`|Defines a symbol for use in conditional compilation. The symbol has file scope. For example, `#define DEBUG`.\n`#undef`|Undefines a symbol.\n### Logical operators in conditional directives\nConditional expressions support and, or, not, equality (`=`), inequality (`<>`), and parentheses. and evaluates to true if both operands are true, or evaluates to true if at least one operand is true, and not negates the value of the operand.\n## Defining and using preprocessor symbols\nPreprocessor symbols in AL are boolean flags that are either defined (evaluating to true) or undefined (evaluating to false). Unlike some other languages, you can't assign specific values to symbols - they're simply on or off.\n### Defining symbols globally in app.json\nSymbols can be defined globally in the app.json file, making them available throughout your extension. The following example defines DEBUG and PROD as global symbols:\n```json\n{\n  \"preprocessorSymbols\": [ \"DEBUG\", \"PROD\" ]\n}\n```\nWhen you define symbols in app.json, they're available in every source file. A local `#undef` directive can make an app.json symbol undefined from that point forward in one file.\n### Defining symbols in code\nYou can define symbols locally within a specific file by using the `#define` directive. Place `#define` and `#undef` directives before the first AL token in the file. Their effect is local to that file.\n```al\n#define TESTING\n#define FEATURE_ENABLED\n\ncodeunit 50100 MyCodeunit\n{\n    trigger OnRun()\n    begin\n#if TESTING\n        Message('This code runs when TESTING is defined');\n#endif\n    end;\n}\n```\n### Undefining symbols\nYou can undefine a symbol by using `#undef`, which makes the symbol undefined from that point forward in the file.\n```al\n#define DEBUG\n// DEBUG is true here\n\n#undef DEBUG\n// DEBUG is now false here\n```\n### How symbols evaluate\n A defined symbol (either in app.json or via `#define`) evaluates to true\n An undefined symbol (never defined, or after `#undef`) evaluates to false\n Use not to check if a symbol is undefined: `#if not DEBUG`\nLearn more about defining extension-wide symbols in ../devenv-json-files.\n## Examples\n### Example 1: Simple conditional compilation\n```al\n#define DEBUG\n\ncodeunit 50100 MyCodeunit\n{\n    trigger OnRun()\n    begin\n#if DEBUG\n        Message('Only in debug versions');\n#endif\n    end;\n}\n```\n### Example 2: Using symbols from app.json\nIf you define symbols in app.json:\n```json\n{\n  \"preprocessorSymbols\": [ \"PREMIUM_FEATURES\" ]\n}\n```\nYou can then use them throughout your code:\n```al\ntable 50100 MyTable\n{\n    fields\n    {\n        field(1; \"Basic Field\"; Code[20]) { }\n\n#if PREMIUM_FEATURES\n        field(2; \"Premium Field\"; Code[20]) { }\n#endif\n    }\n}\n```\n### Example 3: Multiple conditions with logical operators\n```al\n#define DEBUG\n#define TESTING\n\ncodeunit 50101 ConditionalCode\n{\n    trigger OnRun()\n    begin\n#if DEBUG and TESTING\n        Message('Both DEBUG and TESTING are defined');\n#elif DEBUG or TESTING\n        Message('At least one is defined');\n#else\n        Message('Neither DEBUG nor TESTING is defined');\n#endif\n    end;\n}\n```\n### Example 4: Using custom build symbols\nThe compiler doesn't define environment symbols automatically. For each build configuration, add the appropriate custom symbol to preprocessorSymbols in app.json. The following cloud configuration defines CLOUD. For an on-premises configuration, replace CLOUD with ONPREM.\n```json\n{\n  \"preprocessorSymbols\": [ \"CLOUD\" ]\n}\n```\n```al\ncodeunit 50103 EnvironmentSpecificCode\n{\n    trigger OnRun()\n    begin\n#if CLOUD\n        Message('Run cloud-specific logic.');\n#elif ONPREM\n        Message('Run on-premises-specific logic.');\n#else\n        Message('Run default logic.');\n#endif\n    end;\n}\n```\n### Example 5: Using not operator\n```al\n#define PRODUCTION\n\ncodeunit 50102 FeatureToggle\n{\n    trigger OnRun()\n    begin\n#if not PRODUCTION\n        Message('Enable experimental features.');\n#endif\n\n#if PRODUCTION\n        Message('Enable stable features.');\n#endif\n    end;\n}\n```\n## Related information\nDevelopment in AL<../devenv-dev-overview>\nAL development environment<../devenv-reference-overview>\n#conditional-directives\nRegion directive in AL<devenv-directive-region>\nPragma directive in AL<devenv-directive-pragma>\nDeprecating explicit and implicit with statements<../devenv-deprecating-with-statements-overview>\nBest practices for deprecation of code in the Base App<../devenv-deprecation-guidelines>\n../properties/devenv-obsoletestate-property\n../properties/devenv-obsoletereason-property\n../properties/devenv-obsoletetag-property\n../attributes/devenv-obsolete-attribute"}