The list data type can be useful in specific situations when coding in Microsoft Dynamics 365 Business Central. In this post, you will learn what is it and how to use with two use case examples.
- What Is A List Data Type In Business Central?
- Introductions To Lists
- Use Case Example 1
- Use Case Example 2
What Is A List Data Type In Business Central?
Microsoft’s definition of a list is:
Represents a strongly typed list of ordered objects that can be accessed by index. Contrary to the Array data type, a List is unbounded, such that its dimension does not need to be specified upon declaration.
Let´s break it down.
Strongly typed means that a list must have a type defined before using it. It can be a list of texts, a list of decimals, etc.
Just imagine a list as one column of values that have an index starting with 1. In this image you will find at the index 3 the value of Allan. Unbounded means that unlike arrays, we don´t have to specify the dimension. The size or length of the list don´t need to be set when defining it.
Introduction To Lists
If we try to define a list variable like in this image an error will appear:
We need to declare what type of list is it. As we mentioned, it´s strongly typed.
Lists can be created as ‘List of [datatype]’.
However, not all data types can be used. Only simple data types can define a list. Meaning that between all the possibilities listed below, we can only choose some of them.
For instance, if we define a list of variants, an error will show that it´s not a valid type. Same with record, page, codeunit, report or blob.
So, what types can we use for lists?
We can only use simple data types, which include Code, Boolean, Date, DateFormula, DateTime, Decimal, Char, Byte, Text, Time, etc.
Lists have methods built-in to be used. Here are the methods available:
You can find all the information about lists in the Microsoft documentation:
Use Case Example 1
In the following example, you can see how to create a list of emails. Given a string of recipients, we can create a list by using the ‘Split’ method. There are several methods in Business Central that return lists that can help your code.
Then we can loop through each recipient to get the individual emails. After that, for example, we can create individual emails for them, store the values where needed, etc.
The ‘IndexOf’ method returns the index of a certain value in the list. So we can use it to get the values stored previously with ‘Split’.
Use Case Example 2
We can store values with different criteria in a list and use it later. In this example, we can store a list of customers above a certain balance and due balance.
Then, we can send an email or perform any other operation with these customers.
Note that we use ‘List.Contains’ to avoid repeating the same customer. And with ‘Customer.Add’ we filled the list.
Media data types are another useful type that can help you when coding. Learn more about them in the following article:
That´s all. Hope you find it helpful.