1. X++ Maps: It can be used as a temp data store for the given scope of a process. This takes us less over head, and is much quicker than a TempTable.
Map class allows you to associate one value (the key) with another value. Both the key and value can be any valid X++ type, including objects. The types of the key and the value are specified in the declaration of the map. The way in which maps are implemented means that access to the values is very fast.
Below is a sample code that sets and retrieves values from a map.
static void UseOfMaps(Args _args)
{
CustTable custTable;
Map map;
MapEnumerator mapEnumerator;
CustAccount accountNumber;
int counter = 0;
;
map = new Map(Types::String, Types::Integer);
//store into map
while select custTable
{
accountNumber = custTable.AccountNum;
if (!map.exists(accountNumber))
{
map.insert(accountNumber,1);
}
else
{
map.insert(accountNumber,map.lookup(accountNumber)+ 1);
}
}
//retrieve from map by using MapEnumerator
mapEnumerator = map.getEnumerator();
while (mapEnumerator.moveNext())
{
accountNumber = mapEnumerator.currentKey();
info(strfmt("%1,%2",mapEnumerator.currentKey(),mapEnumerator.currentValue()));
}
}
{
CustTable custTable;
Map map;
MapEnumerator mapEnumerator;
CustAccount accountNumber;
int counter = 0;
;
map = new Map(Types::String, Types::Integer);
//store into map
while select custTable
{
accountNumber = custTable.AccountNum;
if (!map.exists(accountNumber))
{
map.insert(accountNumber,1);
}
else
{
map.insert(accountNumber,map.lookup(accountNumber)+ 1);
}
}
//retrieve from map by using MapEnumerator
mapEnumerator = map.getEnumerator();
while (mapEnumerator.moveNext())
{
accountNumber = mapEnumerator.currentKey();
info(strfmt("%1,%2",mapEnumerator.currentKey(),mapEnumerator.currentValue()));
}
}
2. AOT Maps: A map can unify the access to similar columns and methods that are present in multiple tables. You associate a map field with a field in one or more tables. This enables you to use the same field name to access fields with different names in different tables. Methods on maps enable you to create or modify methods that act on the table fields that the map references.
I have created a Map by navigating to AOT>Data Dictionary>Maps and right click and new and gave it name ‘MapTest’
I have created 4 fields in under Fields node in Map (drag and drop from EDT)
Now the next thing I need to do is to associate the fields in map with the fields in different tables, let say I am taking two tables (CustTable and VendTable).
Notice that above, four fields that I have created in Maps also exist in CustTable as well as VendTable with different names.
To associate fields, go to Mapping node, right click it and click New mapping, and enter the table that you want to associate in Mapping Table field. Like
And the associate fields with fields in MAP
Now I have created a method called printInfo under method node in Maps, which print the value of the map field AccNumber.
public void printInfo()
{
info(strFmt(“Map : AccountNum :%1″,this.AccNumber));
}
Similiarly I have create same methods under method nodes of CustTable and VendTable which are printing their respective AccountNumber fields
Now finally I have created a job see below I am not describing every line as I have added comments above the line.
When I run this job see what happens
No comments:
Post a Comment