In the AL language for Microsoft Dynamics 365 Business Central, a List data type is a strongly typed list of ordered objects accessed by index, starting at 1. Lists are a great alternative to arrays. Unlike arrays, lists are unbounded, which means their size doesn’t need to be declared upon creation and can grow dynamically with unlimited capacity.
Lists can only store simple data types such as integers, text, and code. However, lists can be stored within a list, creating nested lists. Lists preserve the sequence in which the elements are added by adding each new element to the end of the list. One important note is that Lists are reference types, which means they are always passed by reference, regardless of whether the parameter is specified as a value or reference (var). If you need to manipulate a copy of the list, you must do a deep copy. Lists are great for holding data in cases where temporary tables are often used. However, lists do not support holding instantiated records, so temporary tables should still be used.
Working with lists is relatively straightforward, and the list data type provides many built-in methods for managing and manipulating data efficiently within the list.
Add Method /AddRange Method
Adds a value to the end of the List.
Adds multiple elements to the end of the list.
procedure AddToList(list: List of [Text])
begin
list.Add('Hello');
list.Add('World');
list.Add('!');
list.AddRange('This', 'is', 'a', 'list', 'of', 'text', 'values');
end;
Reverse Method
Reverses the order of the elements in the List. After items are added to the list, their order can be reversed.
procedure ReverseList(list: List of [Text])
begin
List.Reverse();
end;
Get Method
Gets the element at the specified index and will raise an error if the index is outside the valid list range.
procedure GetElement(list: List of [Text])
var
i: Integer;
element: Text;
begin
for i := 1 to list.Count() do begin
// Two Get Method Options
element := list.Get(i);
list.Get(i, element);
end;
end;
Remove Method/RemoveAt
Removes the first occurrence of a specified element value from the List.
Removes the element at the specified index of the List.
procedure RemoveElement(list: List of [Text])
var
i: Integer;
element: Text;
begin
list.Remove('Hello');
list.RemoveAt(1);
end;
Nested Lists
Nested lists are lists that contain other lists as their elements. You can think of it as one or more lists within a list. This structure allows you to create multi-dimensional data structures, making it easier to represent complex data hierarchies. (with a few other methods for demonstration)
procedure NestedListPlus()
var
list: List of [List of [Text]];
innerList: List of [Text];
i: Integer;
begin
innerList.Add('Hello');
innerList.Add('World');
list.Add(innerList);
list.Get(1).Add('!');
Clear(innerList);
list.Add(innerList);
list.Get(2).Add('Inner');
innerList.Add('List');
if list.Get(1).Contains('Hello') then
i := list.Get(1).IndexOf('Hello');
end;
Overall, the List data type is a powerful tool that streamlines data storage, retrieval, and manipulation in Business Central. By harnessing the power of Lists, developers can significantly enhance code readability and efficiency, making it a valuable resource for specific tasks and scenarios and ultimately boosting productivity.
Read more about the List data type >>here<<.
Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2024 Wave 1 online.