Dynamics 365 FO - Classes with data - Object and Collection classes - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Saturday, September 17, 2022

Dynamics 365 FO - Classes with data - Object and Collection classes

 Dynamics 365 FO - Classes with data

Nghia Song -  Microsoft Dynamics 365 Technical Consultant

Nghia Song

Tel - WhatsApp: +84967324794

Email: songnghia.uit@gmail.com


  1. Object

The objectives are: 

  • Use collection classes to store data in X++.  

  • Extend the RunBase framework to create new batch processes. 

  • Transfer information using the Args object.

  1. Collection classes

X++ contains two compound data types: arrays and containers. They are useful for aggregating values of simpler data types. However, you cannot store objects in arrays or containers. The Microsoft Dynamics 365 collection classes have been designed for storing objects. 

The following collection classes are available: 

  • List 

  • Map 

  • Set 

  • Array 

  • Struct 

The following classes also aide in aggregating data: 

  • RecordSortedList 

  • RecordInsertList

  1. List

A Lis tobject contains members that are accessed sequentially. Lists are structures that can contain members of any X++ type. All the members in the same list must be of the same type.

The following methods are commonly used on List objects: 

  • addEnd(anytype) -adds a member to the end of the list. 

  • addStart(anytype) - adds a member to the start of the list. 

  • elements() -returns the number of members contained in the list. 

  • getEnumerator() -returns a ListEnumerator object for this List object. 

  • typed() -returns the Type that the List contains.

The MapEnumeratorclass allows you to traverse through the members of a map. The following methods are commonly used on MapEnumeratorobjects: 

  • currentKey() -retrieves the key of the pair currently pointed to in the map. 

  • currentValue() -retrieves the value of the pair currently pointed to in the map.

  • moveNext() -moves the enumerator to the next pair in the map. Map enumerators start beforethe first pair in the map, so moveNext() 

must be called to make it point to the first pair in the map. 

  • reset() -moves the enumerator to the start of the map.


Example:

Map mapStateNumbers; 

MapEnumerator enumerator; 

CustTable custTable; 

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

while select custTable 

if(mapStateNumbers.exists(custTable.stateName())) 

mapStateNumbers.insert(custTable.stateName(), 

mapStateNumbers.lookup(custTable.stateName())+1); 

else 

mapStateNumbers.insert(custTable.StateName(), 1); 

enumerator = mapStateNumbers.getEnumerator(); 

while (enumerator.moveNext()) 

info(strfmt("%1 customers are located in %2.", enumerator.currentValue(), enumerator.currentKey())); 

}


  1. Set

A Set is used for the storage and retrieval of data from a collection in which the members are unique. The values of the members serve as the key according to which the data is automatically ordered. Thus, it differs from a List collection class where the members are placed into a specific position, and not ordered automatically by their value.

Set members may be of any X++ type. All members in the set must have the same type.

When a value that is already stored in the set is added again, it is ignored and does not increase the number of members in the set.

The following methods are commonly used on Set objects: 

  •  add(anytype) -inserts a value into the set. 

  • remove(anytype) -removes a value from the set.

  • in(anytype) -determines whether a particular value is a member of the set. 

  • elements() -returns the number of members contained in the set. 

  • getEnumerator() -returns a SetEnumeratorobject for this Set object.

- The SetEnumeratorclass allows you to traverse through the members within a set. The following methods are commonly used on SetEnumerator objects: 

  • current() -retrieves the value currentlypointed to in the set. 

  • moveNext() -moves the enumerator to the next value in the set. Set 

enumerators start beforethe first value in the set, so moveNext() 

must be called to make it point to the first value in the set. 

  • reset() -moves the enumerator to the start of the set.


Example:

Set setOne; 

Set setTwo; 

SetEnumerator enumerator; 

Int value; 

setOne = new Set(types::Integer); 

setOne.add(1); 

setOne.add(2); 

setOne.add(3); 

setTwo = new Set(Types::Integer); 

setTwo.add(3); 

setTwo.add(4); 

setTwo.add(5); 

enumerator = setOne.getEnumerator();

while (enumerator.moveNext()) 

value = enumerator.current(); 

if (setTwo.in(value)) 

setTwo.remove(value); 

}

enumerator = setTwo.getEnumerator();

while (enumerator.moveNext()) 

{

Info(strFmt(‘Value: %1’, value = enumerator.current();));

}

  1. Array

Array objects may hold values of any single type, including objects and records. Objects of this type may be transferred to functions and methods. The values are stored sequentially. 

The following methods are commonly used on Array objects: 

  • exists(int) -checks if a value exists in a specific position in the array

Array objects may hold values of any single type, including objects and records. Objects of this type may be transferred to functions and methods. The values are stored sequentially. 

The following methods are commonly used on Array objects: 

  • exists(int) -checks if a value exists in a specific position in the array

  • lastIndex() -retrieves the highest index that is used to store a value in the array. 

  • value(int _index,[anytype _value])- gets or sets the value of the array member that is stored at the specified index. 

  •  typeId() -returns the data type that the Array contains.

Example

Array array = new Array (Types::Class); 

array.value(1, new Query()); 

array.value(2, new Query()); 

array.value(3, new Query());


int i; 

 // Create an integer array 

 Array ia = new Array (Types::Integer);  

// Write some elements in it 

 for (i = 1; i< 10; i++) 

 { 

        ia.value(i, i*2); 

 }  

// Check the values 

for (i = 1; i< 10; i++) 

        ….. 

  1. Struct

Struct objects (short for structures) are entities that can hold a number of values of any X++ type. Structs are used to group information about a specific entity. For example, there may be a need to store information about a customer's name, group and address and then treat this compound information as one item.

The following methods are commonly used on struct objects: 

  • add(str _fieldName, anytype _value) -adds a new field to the struct and assigns the specified value to it. 

  • exists(str _fieldName) -determines whether a particular field exists in a struct. 

  • fieldName(int _index) -returns the name of the field in the struct at the position specified.

  • fields() -returns the number of fields in the struct. 

  • index(str _fieldName) -calculates the position of a field in the struct based on its name. 

  • remove(str _fieldName) -removes a field from a struct. 

  • value(str _fieldName,anytype _value) -gets or sets the value for a specific field. 

  • valueIndex(int _index, anytype _value) -gets or sets the value of the field at a specific index position in a struct.

Example:

Struct struct = new Struct(); 

int i; 

// Add new fields and values

struct.add("Group", "DOM"); 

struct.add("Name", "Jane Doe"); 

struct.add("Shoesize", 45); 

// Prints the type and name of all items in the struct

for (i = 1 ; i <= struct.fields() ; i++) 

info(strfmt("%1 : %2", struct.fieldType(i), struct.fieldName(i))); 

}

The result in the infolog should be: 

•  String : Group 

•  String : Name 

•  Integer : Shoe size


  1. RecordStortedList

The RecordSortedList class inserts multiple records in a single database trip, and can hold a subset of data from a table in a particular sort order that does not exist as an index.

A RecordSortedList object holds records from a single table. The list has a unique key that is defined by the fields listed by using the sort Order method. Records are automatically sorted as they are inserted, they do not have to be inserted in sort sequence.

RecordSortedList objects are particularly useful for passing a result-set as a parameter.

There is no limit to the size of a RecordSortedListobject, but they are completely memory-based, so there are potential memory consumption problems. 

Use a RecordSortedList in preference to a temporary table when: 

  • Only one sort order is needed. 

  • The number of records is not too high (to avoid memory problems). Compared to temporary tables, RecordSortedListobjects: 

  • Are faster 

  • Are not disk-based 

  • Only have one index 

  • Cannot be used in forms 

  • Require a call between the client and server per (grouped) read

The following methods are commonly used on RecordSortedListobjects: 

  • del(common) -removes a record that has a key that matches the key fields in the recordBuffer from the RecordSortedList. 

  • find(common) -sets the recordBuffer to the contents of the record that has a key that matches the key fields in the recordBuffer, and positions the list to the record returned. 

  • first() -positions the list to the first record in the list, and copies its contents to the recordBuffer.

  • ins(common) -inserts a new record in a RecordSortedList, unless it is a duplicate. 

  • insertDatabase()- inserts multiple records on a single trip to the database. 

  • en()- returns the current number of records in a RecordSortedListobject. 

  • next()- sets the record buffer to the contents of the next record in the RecordSortedListand positions the list to the record returned.

sortOrder(fieldId, ...) -defines the fields on which the records are sorted. 

  • sortOrderFromContainer(container) -defines the field(s) on which the records are sorted.

Example:

RecordSortedList recordSortedList; 

CustTable custTable; 

recordSortedList = new RecordSortedList(tablenum(CustTable));

 

custTable.clear(); 

custTable.AccountNum = "123"; 

custTable.CustGroup = "DOM"; 

custTable.Currency = "USD"; 

recordSortedList.ins(custTable); 


custTable.clear(); 

custTable.AccountNum = "456"; 

custTable.CustGroup = "DOM"; 

custTable.Currency = "USD"; 

recordSortedList.ins(custTable); 


custTable.clear(); 

custTable.AccountNum ="789"; 

custTable.CustGroup ="INT"; 

custTable.Currency ="USD"; 

recordSortedList.ins(custTable); 


recordSortedList.insertDatabase();

  1. RecordInsertList

The RecordInsertList class provides array insert capabilities in the kernel. This allows you to insert more than one record into the database at a time, which reduces communication between the application and the database.

RecordInsertList is similar to RecordSortedList, but it has built-in client/server support (it automatically packs data from one tier to another when needed), and it lacks the sort order features available in RecordSortedList. 

The following methods are commonly used on RecordInsertList objects: 

  • add(common) -inserts a new record in a RecordInsertList.

  • insertDatabase() -inserts multiple records on a single trip to the database

Next: Dynamics 365 FO - Classes with data - Run Base Framework https://www.songnghia.com/2022/09/dynamics-365-fo-classes-with-data-run.html

No comments:

Post a Comment