Mastering String Functions in Business Central: Practical Examples with AL
Mastering String Functions in Business Central: Practical Examples with AL
When working with Microsoft Dynamics 365 Business Central, string manipulation is an essential part of everyday development. From reversing names to formatting messages, AL provides multiple ways to handle text. In this blog, we’ll explore different approaches to string handling through practical examples, including custom logic and built-in AL string functions.
Why String Handling Matters in Business Central
Strings are everywhere—customer names, item descriptions, invoice messages, and more. Being able to manipulate these strings efficiently allows developers to:
Format user-friendly messages
Clean and transform input data
Perform validations
Automate text operations
Let’s dive into some real-world AL examples where we extend the Customer List page with new actions for string manipulation.
Part 1: Custom String Handling Approaches
🔹 Method 1: Using List of Text
We can reverse a string by adding each character into a List of [Text] and then calling .Reverse().
action(CFS_ReverseSelectedCustomerName)
{
Caption = 'Reverse Customer Name';
ApplicationArea = All;
trigger OnAction()
var
StringList: List of [Text];
StringLetter: Text;
ReversedString: Text;
begin
ReversedString := '';
foreach StringLetter in Rec.Name do
StringList.Add(StringLetter);
StringList.Reverse();
foreach StringLetter in StringList do
ReversedString += StringLetter;
Message(ReversedString);
end;
}
This approach is useful when you want more control over the collection of characters.
Output:
Method 2: Using Index Manipulation
Here, we iterate through the string from end to start and build the reversed string.
action(CFS_NewIndex)
{
Caption = 'New Index';
ApplicationArea = All;
trigger OnAction()
var
ReversedString: Text;
i: Integer;
begin
for i := StrLen(Rec.Name) downto 1 do
ReversedString += CopyStr(Rec.Name, i, 1);
Message(ReversedString);
end;
}
Output:
A more direct approach, simple and efficient for reversing text.
Method 3: Using CopyStr
The CopyStr function is perfect for extracting characters one by one.
action(CFS_NewText)
{
Caption = 'New Text';
ApplicationArea = All;
trigger OnAction()
var
ReversedString: Text;
i: Integer;
begin
for i := StrLen(Rec.Name) downto 1 do
ReversedString += CopyStr(Rec.Name, i, 1);
Message(ReversedString);
end;
}
Output:
Part 2: Built-in AL String Functions
Beyond custom logic, AL offers powerful built-in functions for working with text. Let’s explore a few.
Evaluate Examples
The Evaluate function converts text into other datatypes (integer, date, boolean, duration, etc.).
action(CFS_EvaluateExamples)
{
Caption = 'Evaluate Examples';
ApplicationArea = All;
trigger OnAction()
var
MyInt: Integer;
MyDate: Date;
MyBool: Boolean;
MyDuration: Duration;
Value: Text;
OkInt: Boolean;
OkDate: Boolean;
OkBool: Boolean;
OkDur: Boolean;
begin
Value := '150';
OkInt := Evaluate(MyInt, Value);
Value := '2025-09-01';
OkDate := Evaluate(MyDate, Value);
Value := 'TRUE';
OkBool := Evaluate(MyBool, Value);
Value := '3d 5h 45m';
OkDur := Evaluate(MyDuration, Value);
Message(
'Integer = %1 (Ok: %2)\Date = %3 (Ok: %4)\Boolean = %5 (Ok: %6)\Duration = %7 (Ok: %8)',
MyInt, OkInt,
MyDate, OkDate,
MyBool, OkBool,
MyDuration, OkDur);
end;
}
Output:
Super handy when parsing user input or imported text data.
String Functions Examples
Now let’s use some of the most common string functions in AL.
action(CFS_StringFunctions)
{
Caption = 'String Functions';
ApplicationArea = All;
trigger OnAction()
var
SourceTxt: Text[100];
CopyTxt: Text[50];
DelTxt: Text[50];
Len: Integer;
SubstTxt: Text[100];
UpperTxt: Text[50];
LowerTxt: Text[50];
begin
SourceTxt := 'Dynamics 365 Business Central';
CopyTxt := CopyStr(SourceTxt, 1, 8); // "Dynamics"
DelTxt := DelStr(SourceTxt, 1, 9); // "365 Business Central"
Len := StrLen(SourceTxt); // 29
SubstTxt := StrSubstNo('Hello %1, welcome to %2!', 'User', 'Business Central');
UpperTxt := UpperCase(SourceTxt); // "DYNAMICS 365 BUSINESS CENTRAL"
LowerTxt := LowerCase(SourceTxt); // "dynamics 365 business central"
Message(
'Original: %1\CopyStr: %2\DelStr: %3\Length: %4\Substitute: %5\Upper: %6\Lower: %7',
SourceTxt, CopyTxt, DelTxt, Len, SubstTxt, UpperTxt, LowerTxt);
end;
}
Output:
These built-in functions save time and make string handling straightforward.
Final Thoughts
Whether you’re reversing a customer’s name, converting text into other data types, or formatting user-friendly messages, AL’s string manipulation capabilities are both flexible and powerful.
By combining custom logic with built-in functions, you can handle almost any text-related scenario in Business Central.
Try experimenting with these functions in your own extensions—you’ll be surprised how often they come in handy!