Wednesday 22 November 2023

Map as parameters in X++

 We might have some challenges in using map as a parameter in the contract classes. Usually contract classes in SysOperation framework  we may have to have a map as a parameter so the calculated map can be passed to the service classed.

These scenarios might arise mostly in case of Multi threading  where the records will be split and passed in to the contract parameter. Most of the cases we use containers in the contract class and pass the containers as maps and other collection classes are not allowed.

He is a scenario where list of items and prices should be passed in contract class. We cannot pass a map in to contract class and so if we try to use the containers then multiple values cannot be passed.


Below is a code example to achieve this scenario.


Map             map = new Map(Types::String, Types::Real);
    MapEnumerator   me;
    
    container       cnt;
    str             xml;
    XmlDocument     xmlDocument;
 
map.insert("ItemA", 100);
map.insert("ItemB", 200);
map.insert("ItemC", 300);
map.insert("ItemD", 400);
 
    // pack Map to container
    cnt = map.pack();

Now this container can be passed on to the contract class.


//unpack container to map
To get back the map from the container in the service class we can use the below code

   map = Map::create(cnt);


Similar way to get the map as XML and create a map from XML we can use the below code.
 
    // pack/unpack Map to/from XML
    xml         = map.xml();
    xmlDocument = XmlDocument::newXml(xml);
    map         = Map::createFromXML(xmlDocument.root() as XmlNode);

No comments:

Post a Comment