Access Table Record using Map in D365 F&O - X++ Code

Rumman Ansari   Software Engineer   2023-08-29 00:00:00   402  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Access Table Record using Map in D365 F&O - X++ Code



Map map = new Map(Types::Int64, Types::Record);

AtnylaAddress address, address1;

while select address {
    map.add(address.RecId, address);
}

MapEnumerator mapEnum = map.getEnumerator();

while (mapEnum.moveNext()) {
    int64 key = mapEnum.currentKey();
    address1 = mapEnum.currentValue();
    int64 key1 = mapEnum.current();
}

boolean check = map.exists(5637144576);

Map map1 = new Map(Types::Int64, Types::String);

map1.add(1, "A");
map1.add(1, "C");
map1.add(2, "D");

check = map1.exists(2);

if (check) {
    anytype val = map1.lookup(2);
}

Explanation:

  1. A Map named map is created with a key type of Int64 and a value type of Record.
  2. A loop uses the select statement to iterate through the AtnylaAddress table records and adds each record to the map using its RecId as the key.
  3. A MapEnumerator named mapEnum is created to iterate over the elements in the map.
  4. The first while loop moves through the mapEnum, extracting the current key, value (record), and the key again.
  5. The map.exists(5637144576) check determines whether the key 5637144576 exists in the map.
  6. Another Map named map1 is created with a key type of Int64 and a value type of String.
  7. Three key-value pairs are added to map1 using the add method.
  8. The map1.exists(2) check determines whether the key 2 exists in map1.
  9. If the key exists in map1, the lookup method is used to retrieve the value associated with the key (2).>

The code demonstrates the usage of the Map data structure to associate keys with values, and it illustrates basic operations like adding, iterating, checking existence, and looking up values in a map.