Please wait
Talk to Expert
Microsoft Dynamics Business central, Microsoft Dynamics NAV

Dictionary in Business Central.

4

The dictionary is one of the useful tools for developers in Business Central. In this post we will be discussing below points :

  • What is a Dictionary?
  • Introduction to Dictionary.
  • Usage of Dictionary.
  • Use cases

 

What is Dictionary?

A Dictionary is an object type that represents the collection of keys and values. We can think it as a two field table whereas first field represent key and second one represent value.

Keys Value
001 John
002 William
003 Joice

 

The Second characteristic is that dictionaries don’t need to be in order. Contrary to lists, dictionaries can be unordered.

 

Keys Value
002 William
003 Joice
001 john

 

Introduction to Dictionaries:

In AL a dictionary can be defined as following:

As we 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. Here are the list of methods:

Method Name Description
Add(Tkey,Tvalue) Adds the specified key and value to the dictionary.
Containskey(Tkey) Determines whether the Dictionary contains the specified key.
Count() Gets the number of key/value pairs contained in the Dictionary.
Get(TKey, var Tvalue) Gets the value associated with the specified key.
Get(Tkey) Gets the value associated with the specified key.
Keys() Gets a collection containing the keys in the Dictionary.
Remove(Tkey) Removes the value with the specified key from the Dictionary.
Set(Tkey, Tvalue) Sets the value associated with the specified key.
Set(Tkey, Tvalue,var tvalue) Sets the value associated with the specified key.
Values() Gets a collection containing the values in Dictionary.

 

Usage of Dictionary:

Most of the time you can use a dictionary to replace temporary tables. As Microsoft approach, in C/AL , one would have typically used an in-memory temporary table to create a key value data structure.

This kind of approach, when applied, can increase performance significantly. It´s a faster approach than temporary tables.

 

Use cases:

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.

 

 

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.

 

Output:

In this table we can see the virtual representation of what the process is doing.

 

Thank you for investing your time to read our blog! We’re passionate about sharing valuable insights to help you navigate the world of Business Central.

If you’re curious about how we can assist you in reaching your goals, don’t hesitate to get in touch. Our dedicated team is ready to provide support every step of the way.

Let’s turn your vision into reality together!