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:
pageextension 50120 CustomerListExt extends "Customer List"
{
trigger OnOpenPage()
var
TestBoolean: Boolean;
Msg: Label 'TestBoolean: %1\TestBoolean.ToText(): %2\Format(TestBoolean): %3\Format(TestBoolean, 0, 9): %4\Format(TestBoolean, 0, 2): %5';
begin
TestBoolean := true;
Message(Msg, TestBoolean, TestBoolean.ToText(), Format(TestBoolean), Format(TestBoolean, 0, 9), Format(TestBoolean, 0, 2));
TestBoolean := false;
Message(Msg, TestBoolean, TestBoolean.ToText(), Format(TestBoolean), Format(TestBoolean, 0, 9), Format(TestBoolean, 0, 2));
end;
}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
| Operator | Name | Expression | Resulting data type |
|---|---|---|---|
not | Logical negation | not bool | Boolean |
and | Logical and | bool1 and bool2 | Boolean |
or | Logical or | bool1 or bool2 | Boolean |
xor | Exclusive logical or | bool1 xor bool2 | Boolean |
END
Hope this will help.
Thanks for reading.
ZHU




コメント