All Around Microsoft Dynamics Business Central
When working with AL (Dynamics 365 Business Central), we often need to dynamically construct complex filter strings. A common example is filtering users based on their User Security ID. For this purpose, AL provides the TextBuilder, a wrapper around the .NET StringBuilder, optimized for efficient string manipulation.
We want to build a filter for the Access Control table that excludes certain special users. The IDs are read from the User table and concatenated into a filter string separated by |.
local procedure FilterOutSpecialUsers(var AccessControl: Record "Access Control")
var
User: Record User;
UserSelection: Codeunit "User Selection";
FilterTextBuilder: TextBuilder;
begin
UserSelection.FilterSystemUserAndGroupUsers(User);
repeat
FilterTextBuilder.Append(User."User Security ID");
FilterTextBuilder.Append('|');
until User.Next() = 0;
AccessControl.FilterGroup(2);
AccessControl.SetFilter("User Security ID", FilterTextBuilder.ToText().TrimEnd('|'));
AccessControl.FilterGroup(0);
end;
|)local procedure FilterOutSpecialUsers(var AccessControl: Record "Access Control")
var
User: Record User;
UserSelection: Codeunit "User Selection";
FilterTextBuilder: TextBuilder;
begin
UserSelection.FilterSystemUserAndGroupUsers(User);
repeat
if FilterTextBuilder.Length > 0 then
FilterTextBuilder.Append('|');
FilterTextBuilder.Append(User."User Security ID");
until User.Next() = 0;
AccessControl.FilterGroup(2);
AccessControl.SetFilter("User Security ID", FilterTextBuilder.ToText());
AccessControl.FilterGroup(0);
end;
| Criterion | Option 1 | Option 2 |
|---|---|---|
| Append operations | 2 per user | 1–2 per user |
| Extra cost | TrimEnd() |
None |
| Readability | Simple | Slightly more complex |
| Scalability | Medium | High |
For large datasets or performance-critical scenarios, Option 2 is the better choice. It avoids unnecessary operations and works more efficiently with TextBuilder.
Length PropertyThe Length property of .NET’s StringBuilder (and therefore AL’s TextBuilder) does not calculate the length on each access. It simply returns an internal field that is updated during modifications. This means checking Length > 0 is O(1) and extremely fast.
💡 Tip: If you expect thousands of IDs, also check Business Central’s maximum filter length to avoid runtime errors.