Use the ternary operator when coding in the AL language

Enabled for Public preview General availability
Admins, makers, marketers, or analysts, automatically Oct 1, 2024 Oct 1, 2024

Business value

The ternary operator ? : known from other programming languages streamlines conditional operations in code, enhances readability, and reduces verbosity. It's particularly useful for simple conditions, promoting code clarity and intent-focused programming. By allowing variable initialization within the same line, it ensures proper assignment and minimizes code length. With this release, AL now also supports the ternary operator.

Feature details

The ternary operator ? : can now be used to assign one of two values to a variable, depending on the condition of an expression.

Here's an example of using if-then-else as until now and the new ternary operator.

Example of using ternary operator in AL to conditionally assign a variable value.

Tell us what you think

Help us improve Dynamics 365 Business Central by discussing ideas, providing suggestions, and giving feedback. Use the forum at https://aka.ms/bcideas.

See also

Conditional operators (docs)