MADHDA Business Solutions
  • Home
  • About Us
    • Privacy Policy
  • Services
    • Implementations
    • Support
    • Training
    • Upgrade
    • Business Analysis
  • Solutions
    • MS Dynamics 365 Business Central
    • MS Dynamics Nav
    • MS Dynamics 365 Sales/CRM
    • Power BI
    • Power Automation
  • Resources
    • Blog
  • Contact Us
  • How to use “Dictionary” Object type into Business Central.

How to use “Dictionary” Object type into Business Central.

17 February 2023

How to use “Dictionary” Object type into Business Central.

Hello Reader,

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

  • What is 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.

Stay connected with Madhda to learn More!

For more details, pls visit our website www.madhda.com or contact us on Contact@madhda.com

Thanks to all.

Madhda

Previous post: Post Transaction Using Configuration Package in Business Central.
Next post: Change Authentication method from Basic Auth to OAuth2 while accessing Business Central Webservice / APIs.

Categories

  • Microsoft Dynamics 365
  • Microsoft Dynamics Business central
  • Microsoft Dynamics NAV
  • Uncategorized

Recent Post

  • Business Central : Retrieving Azure AD User information via AL.
    March 20, 2023
  • Change Authentication method from Basic Auth to OAuth2 while accessing Business Central Webservice / APIs.
    February 27, 2023
  • How to use "Dictionary" Object type into Business Central.
    February 17, 2023
  • Post Transaction Using Configuration Package in Business Central.
    February 13, 2023
  • How to Import Image from Excel to BC using Azure function.
    February 6, 2023
 


    © Copyright 2020 | All Rights Reserved By MADHDA