Dictionaries can be a useful tool for developers in Microsoft Dynamics 365 Business Central. In this post, you will learn what is it and how to use it. Also, you will find how to boost performance by replacing temporary tables.

  • What Is A Dictionary In Business Central?
  • Introduction To Dictionaries
  • When Should We Use A Dictionary?
  • Use Case Example 1
  • Use Case Example 2

What Is A Dictionary In Business Central?

A dictionary is an object type that represents a collection of keys and values. You can think of it as a two-field table where the first field is the key, and the second field is the value:

The second characteristic is that dictionaries don´t need to be in order. Contrary to lists, that have an ordered index, dictionaries can be unordered. The dictionary above could also look like this:

Introduction To Dictionaries

In AL a dictionary can be defined as the following:

As you can see, there are endless combinations for creating a dictionary. The only restriction is that we can only use simple data types, the same as lists.

Although dictionaries can be more complex to understand than lists, there are only 10 methods available to use whereas lists have 18.

Here is the list of them.

Dictionaries work together with lists. They work together because when you need to retrieve the keys of a dictionary, the keys are actually a list.

So if you haven´t checked the introduction to lists, make sure to do it here:

When Should We Use A Dictionary?

Most of the time you can use a dictionary to replace temporary tables. As Microsoft suggests:

Previously in C/AL, one would have typically used an in-memory temporary table to create a key-value data structure, as shown in the code below. In AL you use the Dictionary Data Type instead.

This kind of approach, when applied, can increase performance significantly. It´s a faster approach than temporary tables. Erik has a great post about it. He compares performance between temporary tables and dictionaries. Make sure to check it out:

Erik Hougaard post: What’s faster than temporary tables?

Use Case Example 1

In the second method of the image below (‘GetCustDictionary’) we can use ‘if CustDictionary.Add() then;‘ to see if a customer can be added to a dictionary. If it´s not already in the dictionary, it will add it.

After that, we return the dictionary and loop through it. With CustDictionary.Keys() we get a list of the keys. Then, we can loop using foreach.

Use Case Example 2

We can use a dictionary inside another dictionary. This way, we can replace a temporary table where more than two fields are needed.

In this example, you can see how we create a dictionary (DetailItemDict) of Description, Base Unit Of Measure and Unit Price. This information is stored in a dictionary that will be part of a bigger one (ItemDictionary).

The bigger one will hold Item numbers in the keys and DetailItemDict in the values.

In this table, you can see a visual representation of what the process is doing. We only use DetailItemDict to store the values for each Item. Then, assign it to the ItemDictionary.

In order to retrieve the values, we have created two nested loops. One for looping through the dictionaries, we do it to get each dictionary that holds the detail values inside. And one to retrieve the datails in each dictionary.

In this video, you can see how it shows the information for the first three items.

You can find all the documentation about dictionaries in the Microsoft documentation below:

Dictionary Data Type – Business Central | Microsoft Learn

That´s all. Hope you find it helpful.

How To Use A Dictionary In Business Central

Post navigation

2 thoughts on “How To Use A Dictionary In Business Central

Leave a Reply

Your email address will not be published. Required fields are marked *