Microsoft Dynamics 365 Business Central – AL Conditional Preprocessor Directives

In the AL programming language, preprocessor directives are used to make code conditional, suppress warnings, and enable code expansion and collapse. The AL preprocessor directives are grouped into conditional, regions, and pragmas categories. In this article, I’ll highlight Conditional Preprocessor Directives.

Conditional preprocessor directives are a feature in AL that allows developers to include or exclude parts of the code based on certain conditions. These directives are evaluated at compile-time and help manage different configurations or enable and disable certain features.

A conditional directive checks the value of a symbol to determine if code is included in the compilation. Symbols may be defined at the beginning of a source file to set the Symbol for the scope of that file, or they can be defined in the app.json file for global scope within the extension.

Symbols are defined globally with the preprocessorSymbols setting in the extensions App.json file.

Symbols are defined with the scope of a file with the #define directive at the beginning of a source file. Symbols may also be undefined with the #undef directive, which is useful in the case of a global directive that should not be applied to a specific file.

Conditional preprocessor directives in AL for Microsoft Dynamics 365 Business Central may also use the Logical Operators “AND” and “OR,” allowing developers to include or exclude code based on multiple preprocessor symbols within one conditional directive. (Thank you for the comment, navfreak.com)

#define SYMBOL2
#define SYMBOL3
codeunit 50102 "DVLPR My Stuff"
{
    procedure CompilerDirective()
    var
        MessageTxt: Text;
    begin
#if SYMBOL1 or SYMBOL2        // Code to include if Symbol1 or Symbol2 is defined
        MessageTxt += 'Symbol1 or Symbol2\';
#endif

#if SYMBOL2 and SYMBOL3      // Code to include if Symbol2 and Symbol3 are defined
        MessageTxt += 'Symbol2 and Symbol3';
#endif

        Message(MessageTxt);
    end;


Using Conditional Preprocessor Directives is considered Best Practices for Deprecation of AL Code.

Read more about the feature Conditional directives here.

Note: The code and information discussed in this article is for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Leave a Reply

Your email address will not be published.