Dynamics 365 Business Central: Formatting Boolean Values (Yes/No, true/false, and 1/0)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, formatting Boolean Values (Yes/No, true/false, and 1/0).

In Dynamics 365 Business Central (AL language), the Boolean data type is a fundamental primitive type used to store logical values.
Boolean data type: Indicates true or false.

When creating a table, a Boolean field in the database is automatically rendered as a Checkbox/Toggle on the page (UI) by default, for example,
List page:

Card page:

There’s a minor issue here: how do we handle displaying ‘Yes/No’ or ‘true/false’ on pages or reports? While we could hardcode it, that’s definitely not recommended.

Actually, it’s not that difficult. I’ve summarized a few ways to convert Boolean values to text, and I hope this helps.

TestBoolean := true

TestBoolean := false

Test code:

Very simple. Give it a try!!!😁

PS:
1. Naming Conventions: Use prefixes that imply a state or a question to make code readable. Examples: IsPosted, HasLines, AllowEditing, EnableSync.

2. Avoid Redundant Comparisons: Since a Boolean is already a truth value, do not compare it to true or false unnecessarily.

  • Bad: if IsPosted = true then
  • Good: if IsPosted then
  • Good: if not IsPosted then

3. Boolean (logical) operators

OperatorNameExpressionResulting data type
notLogical negationnot boolBoolean
andLogical andbool1 and bool2Boolean
orLogical orbool1 or bool2Boolean
xorExclusive logical orbool1 xor bool2Boolean

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL