Microsoft Dynamics 365 Business Central: Decoding Dictionaries: Unlocking the Power of Key-Value Pairs

In the AL language for Microsoft Dynamics 365 Business Central, the Dictionary data type represents an unordered collection of keys and values optimized for fast lookup of values. In a Dictionary, each key is unique and is used to access its corresponding value. The dictionary data type allows you to store key-value pairs where there is a logical association between the pairs or when you need fast data retrieval based on a custom key. Unlike lists indexed by a range of numbers, dictionaries are indexed by keys.

It is important to note that every key in a Dictionary must be unique. A key cannot be null, but a value can be null when the value type is a reference type.

To create a new Dictionary that contains the same values as the original Dictionary, you perform a shallow copy by iterating through the keys of the original Dictionary and adding them to the new Dictionary.

The Dictionary data type does not support holding instantiated records. For this purpose, a developer should use a temporary table instead.

Add

Adds the specified key and value to the dictionary

 procedure AddDictionaryEntry(SalesDictionary: Dictionary of [Text, Decimal])
    begin
        SalesDictionary.Add('10000', 1234.56);
        SalesDictionary.Add('20000', 9564.22);
        SalesDictionary.Add('30000', 789.12);
        SalesDictionary.Add('40000', 456.78);
    end;

ContainsKey

Determines whether the Dictionary contains the specified key.

    procedure ContainsKey(SalesDictionary: Dictionary of [Text, Decimal])
    var
        ContainsKey: Boolean;

    begin
        ContainsKey := SalesDictionary.ContainsKey('10000');
    end;

Count

Gets the number of key/value pairs contained in the Dictionary.

    procedure KeyCount(SalesDictionary: Dictionary of [Text, Decimal]): Integer
    var
        Count: Integer;

    begin
        Count := SalesDictionary.Count();
        exit(Count);
    end;

Get

Gets the value associated with the specified key.

    procedure GetValue(SalesDictionary: Dictionary of [Text, Decimal]; dictkey: Text): Decimal
    var
        dictvalue: Decimal;
    begin
        // Get will error if the key does not exist
        // You can use contains to check if the key exists
        if SalesDictionary.ContainsKey(dictkey) then
            SalesDictionary.Get(dictkey, dictvalue);

        dictvalue := SalesDictionary.Get(dictkey);

        exit(dictvalue);
    end;

Keys / Values

Gets a collection containing the keys / Values in the Dictionary.

    procedure GetKeysValues(SalesDictionary: Dictionary of [Text, Decimal]; KeysList: List of [Text]; ValuesList: List of [Decimal])
    begin
        KeysList := SalesDictionary.Keys();
        ValuesList := SalesDictionary.Values();
    end;

Remove

Removes the value with the specified key from the Dictionary.

    procedure RemoveKey(SalesDictionary: Dictionary of [Text, Decimal])
    begin
        SalesDictionary.Remove('40000');
    end;

The Dictionary data type is optimized for lookups, allowing for efficient storage and retrieval of values without the need to query the database repeatedly.

Overall, the Dictionary data type in Business Central is not just a powerful tool but a crucial one for efficiently storing and retrieving key-value pairs. It is an asset for developers, underscoring the significance of their work with data structures in AL code.

Read more about the Dictionary 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.

Permanent link to this article: https://www.dvlprlife.com/2024/07/microsoft-dynamics-365-business-central-decoding-dictionaries-unlocking-the-power-of-key-value-pairs/

Leave a Reply

Your email address will not be published.