Access Table Record using Map in D365 F&O - X++ Code
☰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:
-
A
Map
namedmap
is created with a key type ofInt64
and a value type ofRecord
. -
A loop uses the
select
statement to iterate through theAtnylaAddress
table records and adds each record to themap
using itsRecId
as the key. -
A
MapEnumerator
namedmapEnum
is created to iterate over the elements in themap
. -
The first
while
loop moves through themapEnum
, extracting the current key, value (record), and the key again. -
The
map.exists(5637144576)
check determines whether the key 5637144576 exists in the map. -
Another
Map
namedmap1
is created with a key type ofInt64
and a value type ofString
. -
Three key-value pairs are added to
map1
using theadd
method. -
The
map1.exists(2)
check determines whether the key 2 exists inmap1
. -
If the key exists in
map1
, thelookup
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.