Friday, 15 March 2019

Map - Collection in X++

Definition

The Map class allows you to associate one value (the key) with another value. Both the key and value can be any valid X++ type. 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.

The (key, value) pairs in a map can be traversed using the MapEnumerator class.

Remarks:

Multiple keys can map to the same value, but one key can map to only one value at a time. If you add a (key, value) pair with an existing key value, it will replace the existing pair with that key value.

Syntax:

Map   map = new Map (Types::Integer, Types::String);

Types::Integer – Key - any type

Types::String - Values added only same type.


How to create map using X++

static void SampleMap(Args _args)
{
    // Declaration of the map or creating map
    Map map = new Map (Types::Integer, Types::String);
}

Use of definitionString method

A string that contains a definition of the map. 

static void SampleMap(Args _args)
{
    // Declaration
    Map map = new Map (Types::Integer, Types::String);
   
    info (strFmt ("Definition String %1 ",map.definitionString()));
   
}
Output:
Definition String [int -> str]

Insert into a Map
static void SampleMap (Args _args)
{
    Map map = new Map (Types::Integer, Types::String);
    // inserting key and values
    map.insert (1,"Ram");
    map.insert (2,"Mani");
    map.insert (3,"Ravi");
    map.insert (4,"Anand");
    map.insert (5,"Siva");

    if (! map.empty ())
    {
        info (strFmt ("No of elements %1 ", map.elements ()));
    }

}
Output:
No of elements 5

Use of domainSet method
Creates a set of the key (domain) values in a map
static void SampleMap (Args _args)
{
    Set               set;
    SetEnumerator      enumerator;
    Map map = new Map (Types::Integer, Types::String);
    map.insert (1,"Ram");
    map.insert (2,"Mani");
    map.insert (3,"Ravi");
    map.insert (4,"Anand");
    map.insert (5,"Siva");

    set= map.domainSet();
    enumerator = set.getEnumerator();

    while (enumerator.moveNext())
    {
            info(strFmt(" domain value %1",enumerator.current()));

    }

}

Output


Traverse the elements in the map:
static void SampleMap(Args _args)
{
    Map map = new Map (Types::Integer, Types::String);
    MapEnumerator   mapEnum;

    map.insert(1,"Ram");
    map.insert(2,"Mani");
    map.insert(3,"Ravi");
    map.insert(4,"Anand");
    map.insert(5,"Siva");
  
    if(!map.empty())
    {
        info(strFmt("No of elements in the map is %1 ", map.elements()));
    }

    mapEnum = map.getEnumerator();
    while (mapEnum.moveNext())
    {
        info(strFmt("Value %1 for key %2                              ",mapEnum.currentValue(),mapEnum.currentKey()));
    }
}


Duplicate
static void SampleMap(Args _args)
{
    Map map = new Map(Types::Integer,Types::String);

    MapEnumerator   mapEnum;

    map.insert(1,"Ram");
    map.insert(2,"Mani");
    map.insert(3,"Ravi");
    map.insert(3,"Anand");
    map.insert(5,"Siva");

     if(!map.empty())
    {
        info(strFmt("No of elements in the map is  %1 ", map.elements()));
    }

    mapEnum = map.getEnumerator();
    while (mapEnum.moveNext())
    {
        info(strFmt("Value %1 for key %2 ", mapEnum.currentValue(), mapEnum.currentKey()));
    }

}

Output
No of elements in the map is 4
Value Ram for key 1
Value Mani for key 2
Value Anand for key 3
Value Siva for key 5

Find – lookup
static void SampleMap(Args _args)
{
    Map map = new Map(Types::Integer, Types::String);
   
    map.insert(1,"Ram");
    map.insert(2,"Mani");
    map.insert(3,"Ravi");
    map.insert(4,"Anand");
    map.insert(5,"Siva");

    if(!map.empty())
    {
        info(strFmt(" Look up value is %1",map.lookup("3")));
    }
}

Output:
Look up value is Ravi


Remove element – delete
static void SampleMap(Args _args)
{
    Map map = new Map(Types::String,Types::String);
    MapEnumerator   mapEnum;
   
    map.insert("Ram","Tamil");
    map.insert("Mani","English");
    map.insert("Ravi","Maths");
    map.insert("Anand","English");
       
    if(map.remove("Mani"))
    {
        mapEnum = map.getEnumerator();
        while (mapEnum.moveNext())
        {
            info(strFmt("Value %1 for key %2 ",                         mapEnum.currentValue(),mapEnum.currentKey()));
        }
    }
}

Output
Value English for key Anand
Value Tamil for key Ram
Value Maths for key Ravi

How to create the AOT Map:
*      Create tables :
1. MapVendTable – Fields – AccountNum (String), BankAccount (String), etc…
2. MapCustTable – Fields – AccountNum (String), BankAccount (String), etc…

*      2.Create a Map
1. Under AOT- Data Dictionary – Maps, add a Map.
2. We can create common fields in the CustTable and VendTable.


3. Create new mapping and select related table – VendTable
4. Create new mapping under the mapping node and select the mapping table

5. Two more tables added and mapped in the under mapping node. 

6. Create a test job

Code:

static void SampleMap(Args _args)
{
    // map
    MapVendCustTable       mapVendCustTable;
    // table
    MapCustTable           mapCustTable;
    MapVendTable           mapVendTable;

        // customer part
   
    mapVendCustTable = mapCustTable;
    mapVendCustTable.AccountNum ="0001";
    mapVendCustTable.DlvMode    ="Truck-Truc";
    mapVendCustTable.DlvTerm    = "CFR";
    mapVendCustTable.Currency   = "INR";
    mapVendCustTable.TaxGroup   = "CALA";
    mapVendCustTable.insert();
  

    while select  mapVendCustTable
    {
        info(strFmt("Vendor Account  is %1 ",mapVendCustTable.AccountNum));
        info(strFmt("Vendor Currency  is %1 ",mapVendCustTable.Currency));
        next mapVendCustTable;
    }

    //Vendor part

    mapVendCustTable = mapVendTable;
    mapVendCustTable.AccountNum ="0002";
    mapVendCustTable.DlvMode    ="Truck-Truc";
    mapVendCustTable.DlvTerm    = "CFR";
    mapVendCustTable.Currency   = "INR";
    mapVendCustTable.TaxGroup   = "CALA";
    mapVendCustTable.insert();

    while select mapVendCustTable
    {
        info(strFmt("Customer Account is %1 ",mapVendCustTable.AccountNum));
        info(strFmt("Customer Currency is %1 ",mapVendCustTable.Currency));
        next mapVendCustTable;
    }
}


Table browser result:
MapVendTable browser.

MapCustTable browser

No comments:

Post a Comment

Step-by-Step Guide to Restore a SQL BACPAC File - Microsoft dynamics D365 Fin & Ops

 Restore steps for bacpac file in to SQL server - Microsoft dynamics D365 Fin & Ops. Log in to LCS and navigate to the asset library. On...