Monday, July 30, 2018

Collection classes


The X++ language syntax provides two compound types: arrays and containers.
They are useful for aggregating values of simpler types. However, you cannot store objects in arrays or containers. 

The Microsoft Dynamics AX collection classes have been designed for storing objects. The classes are implemented in C++ to achieve the maximum performance (they are system classes).

Collection classes were formerly called Foundation classes.

ClassDescriptionData typesAccess
ListValues stored sequentially with ability to add values at the beginning or end of the listAll values must be the same data typeUse ListIterator or ListEnumerator
SetValues stored non-sequentially. Duplicates are not added to the set.All values must be the same data typeUse SetIterator or SetEnumerator
MapValues stored by using a unique key.The key and the value need not be from the same data type.Use MapIterator or MapEnumerator
StructValues stored by using a unique string as a keyThe key must be a string. The values can be of any type.Provide the string key to the value method

List -----
  • A list stores objects of all the same type in a sequential order. 
  • The elements can be any type specified in the Types base enum. 
  • Elements may be added to the beginning or end of the list. 
Usefull list methods:
--------------------------------------------------------------------------------------
addEnd(_item) -- Adds _item to the end of the list
addStart(_item) -- Adds _item to the beginning of the list
appendList(_list) -- Adds elements _list to the end of current list
elements                 -- Returns the number of items in the list
getEnumerator -- Returns an enumerator for traversing the list

Example:
-----------------------------------------------------------------------------------------
public void listExample() {
    List listOne;
    List listTwo;
    List combinedList;
    ListEnumerator listEnumerator;
    SalesTable salesTable;

    // Initialize using Types enum
    listOne = new List(Types::String);

    // Initialize using extendedTypeId2Type which does not require
    // knowing the base type
    listTwo = new List(extendedTypeId2Type(extendedTypeNum(SalesId)));

    // Add a and b to list such that order is b,a
    listOne.addEnd("a");
    listOne.addStart("b");

    // Add an actual sales id and dummy sales id
    select firstOnly SalesId from salesTable;
    listTwo.addEnd(SalesTable.SalesId);
    listTwo.addEnd("Dummy_Sales_Id_12345");

    // Print first list
    listEnumerator = listOne.getEnumerator();
    info("List one");
    while (listEnumerator.moveNext()) {
        info(listEnumerator.current());
    }

    // Create a new list by combining lists
    combinedList = List::merge(listTwo, listOne);

    // Print combined list
    info("Combined");
    listEnumerator = combinedList.getEnumerator();
    while (listEnumerator.moveNext()) {
        info(listEnumerator.current());
    }
}
Output:
image001

Set :
----------------------------------------------------------------
A set is an unordered collection that contains elements of all the same type and is similar to a mathematical set in that each element must be unique. 
Again, the types are all the possible types of the Types base enum. 
Repeated inserts of a value that is already in the set will have no effect. 
There will be no record of how many such inserts of a duplicate value occurred. 
For example, if a set of Types::Integer contains only the integer 2, repeated calls to the add method with 2 as a parameter will have no effect. No matter how many such calls are made, the set will still only contain the integer 2. 
One possible use of a set is to store RecIds to ignore any duplicate records and insure a process only acts once per record.
Unlike a list, an element can be removed from a set directly without using an iterator. There are also powerful methods that mimic mathematical set operations that allow creation of new sets based on two existing methods. 
Set Methods:
-----------------------------------------------------------------------------
add(_item)                           --------Adds _item to the set
delete(_item)                    --------Removes the passed in item to the set
elements                          ---------Returns the number of items in the set
getEnumerator                 ----------Returns an enumerator for traversing the set
in(_item)                         ----------Returns true if _item is present in the set
difference(_set1, _set2) ----------Returns items that are in _set1 but are not in _set2
intersection(_set1, _set2) ----------Returns a new set that contains only elements present in both _set1 and _set2
union(_set1, _set2)        ----------Returns a new set that contains all the elements of _set1 and _set2.
Example:
------------------------------------------------------------------------------------------------------
public void setExample() {
    Set setOne;
    Set setTwo;

    // Inner method for printing sets
    void printSet(Set _set, str _prefix) {
        int i;
        SetEnumerator setEnumerator;
        str output = _prefix;

        setEnumerator = _set.getEnumerator();

        // Add each element to output string 
        while (setEnumerator.moveNext()) {
            output += strfmt(" %1", setEnumerator.current());
        }
        info(output);
    }

    // Declare sets
    setOne = new Set(Types::String);
    setTwo = new Set(Types::String);

    // Fill sets
    setOne.add("A");
    setOne.add("B");
    setTwo.add("B");
    setTwo.add("C");

    // Size of set
    info(strfmt("Size of set one: %1", setOne.elements()));

    // Print sets and the results of set operations
    printSet(setOne, "Set 1");
    printSet(setTwo, "Set 2");
    printSet(Set::difference(setOne, setTwo), "Difference ");
    printSet(Set::intersection(setOne, setTwo), "Intersection ");
    printSet(Set::union(setOne, setTwo), "Union");
}
Output:
---------------------------------------------------------------------------
image003

Map

A map stores key-value pairs. For each key, there is a corresponding value.
Accessing the corresponding value of a key is a fast operation. 
The keys are unique and are all of the same type. The values do not have to be unique and also are all the same type. As with the other collection classes, the possible types are those specified by the Types base enum. Note that the key types and value types do not need to be the same and typically are not.
Some examples of maps are keys that are RecIds or strings that correspond to a numeric value. Another example is mapping RecIds from one table to a Record of another table, thus quickly and easily creating a fast performing link between two tables without having to use a temp table. The diagram below shows a map with Int64 keys mapping to reals.
image004
Useful map methods are summarized in the table below:
delete(_key)Deletes _key (and thereby its corresponding value) from the map
elementsReturns the number of key-value pairs in the map.
exists(_key)Returns true if _key is present in the map.
insert(_key, _value)Inserts the key value pair with a key of _key and a corresponding value of _value. In other words, lookups for _key will return _value.If _key already existed in the map, the corresponding value of that key is overwritten such that the new mapped value is _value.
lookup(_key)Returns the corresponding value of _key.
One important note is that calling the lookup method for a key that does not exist in the map will cause a runtime error. Therefore, the exists method should always be called first to verify that the key exists.
The MapEnumerator class traverses a map. The key and value are retrieved from the enumerator using the currentKey and currentValue methods.
The code below shows examples of several of the map methods including using an enumerator:
public void mapExample() {
    Map theMap;
    MapEnumerator mapEnumerator;
    real value;

    // Declare map
    theMap = new Map(Types::String, Types::Real);

    // Insert values into the map
    theMap.insert("LOT-01", 42.5);
    theMap.insert("LOT-02", 58.3);

    // Insert new value for LOT-01.  Previous value overwritten.
    theMap.insert("LOT-01", 99.9);

    // Traverse the map
    mapEnumerator = theMap.getEnumerator();
    while (mapEnumerator.moveNext()) {
        info(strfmt("key %1 value %2",
            mapEnumerator.currentKey(),
            mapEnumerator.currentValue()));
    }

    // Check if key exists
    info(strfmt("Does LOT-01 exist? %1", theMap.exists("LOT-01")));

    info("Checking if LOT-03 exists");
    if (theMap.exists("LOT-03")) {
        // Will not execute this code, so there is no error
        value = theMap.lookup("LOT-03");
    }

    // Will cause run time error: Lookup a key that is not in the map without 
    // first calling exists
    value = theMap.lookup("LOT-03");

    value = 1;
}
The code above produces the following output:
image005

Struct

struct groups information into a single entity. Any of the types of the Types enum can be stored which includes containers, dates, Int64’s, integers, objects, real numbers, records, strings, times, and UTCDateTimes. The diagram of the struct named “custInfo” shows an example struct that collects a string, real number, record, and object into a single entity.
Capture
The struct class is one of the simplest collection classes. Using a struct is similar to using a container that has a small number of values at fixed positions (such as the TradeTotals class). One major difference is that a struct is a class while a container is a built-in data type. A struct uses string indexes whereas a container uses numeric indexes. One of the problems with both is that the indexes or keys need to be managed entirely by the developer. For example, in the struct illustrated above, the value “43.58” needs to be accessed by providing the string “invoiced” exactly. Using the string “invoice” will cause a run time error because no value exists for the key “invoice”. For both structs and containers, one way to manage this issue is to access the keys/indexes using #define or methods.
The code below uses a constant for the invoiced but not the other keys. The code shows how to create and fill the struct illustrated above as well as how to iterate through a struct.
public void structExample() {#
    define.invoiced('invoiced')

    Struct theStruct;
    SalesTable salesTable;
    int i;

    // Create struct
    theStruct = new Struct(
        Types::String, "name",
        Types::Real, #invoiced,
        Types::Record, "order");

    // Fill struct
    theStruct.value("name", "John Smith");
    theStruct.value(#invoiced, 43.58);
    select firstOnly salesTable;
    theStruct.value("order", salesTable);

    // Add a new entry into the struct
    theStruct.add("orderType", SalesTableType::construct(theStruct.value("order")));

    // Print struct information
    info(strFmt("Definition string: %1", theStruct.definitionString()));
    for (i = 1; i <= theStruct.fields(); i++) {
        info(strFmt("Field name: %1 Type: %2",
            theStruct.fieldType(i),
            theStruct.fieldName(i)));
    }

    // Using exists for a key
    info(strFmt("Exists for key 'xyz'? %1", theStruct.exists("xyz")));
}
Below is the output produced by the above code:
image007
The sections below outline aspects of collection classes that may apply to more than one collection class.

Iterators vs. Enumerators

Enumerators should always be used to traverse a list, set, or map unless elements need to be deleted. Iterators can fail if they are used on a different tier than the tier where they were created. For example, code will fail if an iterator was created on the client side, but then runs on the server side or vice versa. Also, loop maintenance is easier when using an enumerator since there is no need to advance the iterator as a separate step.
while(iterator.more())     while (enumerator.moveNext())
{                          {
 iterator.next();   }
}
If the client/server problem can be safely avoided, then iterators can be used for deletion. However, there are alternative solutions (potentially expensive) to deleting elements from a collection without using an iterator. For a set, one possibility is putting elements to delete in a second set during traversal and calling the static intersection method. For a map, one could put keys to be deleted in a set, then traverse the set, deleting each key individually.

Pack and Create

All the collection classes mentioned above can be packed into a container using the pack method. A new instance of these classes can then be created using the appropriate static create method.
public void packCreateExample() {
    List aList = new List(Types::Real);
    List aNewList;
    container c;

    // Fill list
    aList.addEnd(1.61803);
    aList.addEnd(3.14159);

    // Pack the list into a container
    c = aList.pack();

    // Create a new list from the packed list
    aNewList = List::create(c);
}

Uniqueness of Tables and Classes

Special care should be taken if tables or classes need to be unique within the collection classes. This includes elements in a set or keys of a map. If you use a table as a unique key to a map, it seems that RecId is not the field used to determine uniqueness. Instead, non-system fields are checked. One way to bypass this is to add a GUID field to the table (thanks to Justin Wong for pointing this out). For classes it seems that even if two objects have the same values for variables, the objects will be determined to be unique by the collection classes.

Sorting Behavior of Sets and Maps

No matter what order elements are added to a set or keys are added to a map, when using an enumerator to traverse the set or map, the elements are in a sorted order (for string and numeric types). However, the MSDN documentation for the set class states that elements are stored in a set “in a way that facilitates efficient lookup of the elements.” It might not be safe, therefore, to rely on this sorting behavior as it might possibly change in the future.
The code below shows the sorting behavior in a set of reals:
public void setSortingExample() {
    Set set = new Set(Types::Real);
    SetEnumerator setEnumerator;

    set.add(2.3);
    set.add(3.8);
    set.add(1.2);

    setEnumerator = set.getEnumerator();
    while (setEnumerator.moveNext()) {
        info(setEnumerator.current());
    }
}
The result of this code is shown below:
image008

How to deploy custom code on D365 ?

Overview of the process 


In order to deploy your code and customizations to a runtime environment (Demo, Sandbox or Production), you must create deployable packages of your solution or implementation. Deployable packages can be created using the Visual Studio dev tools, or by the build automation process that are available on build environments. These deployable packages are referred to as Application Deployable Packages or AOT Deployable Packages. The image below is an overview of the process. Once a deployable package is created, it must be uploaded to the LCS project's asset library. An administrator can then go to the LCS environment page and apply the package to a runtime environment using the Maintain > Apply updates tool.

Create and apply a deployment package

Note : Application Deployable Packages do not contain source code.

Create a deployable package

After you have completed the development stage, follow these steps to create a deployable package from Visual Studio.
  1. In Microsoft Visual Studio, select Dynamics 365 > Deploy > Create Deployment PackageCreate deployment package
  2. Select the packages that contain your models, and then select a location in which to create the deployable package. Select a location
  3. After a deployable package is created, sign in to Microsoft Dynamics Lifecycle Services (LCS), and then, in your LCS project, click the Asset Library tile.
  4. Upload the deployable package that you created earlier.

Apply a deployable package

Apply updates to a cloud environment :

Note :Applying packages causes system downtime. All relevant services will be stopped, and you won't be able to use your environments while the package is being applied. You should plan accordingly.

Supported environments

The following topologies support package deployment that uses automated flows in LCS:
  • LCS Implementation Project – All environment types are supported. Automated package application is a self-service operation in all environments except production environments. For production environments, customers must use LCS to submit a request to apply packages.
  • LCS Partner and Trial Projects – All environment types are supported, except multi-box dev/test topologies.
For other topologies (below), you must use Remote Desktop Protocol (RDP) to connect to the environment and install from the command line. 

Source:MSDN

Difference between static method and instance method

Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in within which it defined.
public void geek(String name)
{
 // code to be executed....
}
// Return type can be int, float String or user defined data type.
Memory allocation: These methods themselves are stored in Permanent Generation space of heap but the parameters (arguments passed to them) and their local variables and the value to be returned are allocated in stack. They can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depend on the access typeprovided to the desired instance method.
Important Points:
  • Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
  • Every individual Object created from the class has its own copy of the instance method(s) of that class.
  • They can be overridden since they are resolved using dynamic binding at run time.
class Foo{
     
    String name = "";
     
    // Instance method to be called within the same class or
    // from a another class defined in the same package
    // or in different package.
    public void geek(String name){
         
        this.name = name;
    }
}
 
class GFG {
    public static void main (String[] args) {
     
        // create an instance of the class.
        Foo ob = new Foo();
          
        // calling an instance method in the class 'Foo'.
        ob.geek("GeeksforGeeks");
        System.out.println(ob.name);
    }
}

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
public static void geek(String name)
{
 // code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

Memory Allocation: They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Important Points:
  • Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
  • They are designed with aim to be shared among all Objects created from the same class.
  • Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
public static void geek(String name)
{
 // code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

Memory Allocation: They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Important Points:
  • Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
  • They are designed with aim to be shared among all Objects created from the same class.
  • Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.
class Geek{
     
    public static String geekName = "";
     
    public static void geek(String name){
         
        geekName = name;
    }
}
 
class GFG {
    public static void main (String[] args) {
         
        // Accessing the static method geek() and
        // field by class name itself.
        Geek.geek("vaibhav");
        System.out.println(Geek.geekName);
        
        // Accessing the static method geek() by using Object's reference.
        Geek obj = new Geek();
        obj.geek("mohit");
        System.out.println(obj.geekName);  
         
        
    }
}


When to use static methods ??
  • When you have code that can be shared across all instances of the same class, put that portion of code into static method.
  • They are basically used to access static field(s) of the class.
 Instance method vs Static method
  • Instance method can access the instance methods and instance variables directly.
  • Instance method can access static variables and static methods directly.
  • Static methods can access the static variables and static methods directly.
  • Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...