web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

Ternary Operator in Business Central (AL) – When to Use (and When Not To)

Aman Kakkar Profile Picture Aman Kakkar 3,422 Super User 2026 Season 1


We are seeing AL Language getting mature day-by-day, and just like other programming languages (C#, C++, Java, etc) - AL also supports Ternary Operator since wave 2 (BC25).


Ternary Operator makes your code more compact and readable in certain scenarios, but it works just like IF-ELSE statements.


So, the question remains the same - Should I use IF-ELSE or the ternary operator?


Both approaches help in assigning values based on conditions, but they differ in readability, use cases, and limitations.


Let’s break it down.




Ternary Operator – The Compact Way

The ternary operator allows you to evaluate a condition and return a value in a single line.

A simple syntax for ternary operator is -

Result := Condition ? ValueIfTrue : ValueIfFalse;


Let’s look at some examples.




Example 01: Simple Assignment

Description := Item.Get("Item No.") ? Item.Description : '';


This is same as -


If Item.Get("Item No.") then
    Description := Item.Description
else 
    Description := '';


Output:

  • If Item exists → returns Item Description

  • If not → returns blank



Example 02: Default Value Handling

Rec."Line No." := POLine.FindLast() ? POLine."Line No." + 10000 : 10000;

Output:

  • If previous line exists → increments Line No.
  • If not → starts from 10000



Example 03: Conditional Calculation

Discount := Amount > 1000 ? 10 : 0;
Output:
  • Returns 10 if Amount > 1000
  • Otherwise returns 0



Best for: Simple conditions, single assignments, default values.




IF-ELSE – The More Readable Approach


The traditional IF-ELSE block is more verbose but gives better control and clarity.


Example 01: Same Logic Using IF-ELSE

if POLine.FindLast() then
    Rec."Line No." := POLine."Line No." + 10000
else
    Rec."Line No." := 10000;



Where Ternary Falls Short

While ternary is powerful, it is not suitable in all scenarios. For example -


Example 01: Multiple Statements

if Item.Get("Item No.") then begin
    Description := Item.Description;
    Validate("Unit Price");
end;

Ternary cannot handle multiple operations at once. This is one of the most important drawbacks of using ternary operator.




Example 02: Complex Conditions

Result := A ? (B ? X : Y) : Z;

This becomes difficult to read and maintain. The developers who have never worked on the Ternary operator, will get difficulty understanding such complex conditions.



Which One Should You Use?

Use ternary whenever possible → it’s cleaner and reduces code lines.

Use IF-ELSE when you need → multiple statements, validations, or complex logic.


Think of it this way:

Ternary = Simple, Clean, Compact

IF-ELSE = Flexible, Readable, Controlled


The decision should be based on readability and maintainability, not performance.




Conclusion

I generally use Ternary Operator a lot - using it for finding the last Line No., or assigning Description value when Code or "No." field is validated. Everyone should use ternary operator whenever and wherever they can, but it should be readable and understandable.


For the developers who never worked on Ternary Operator before, it should not be a headache for them to understand simple conditions. 

code snippet widget

Comments