Tuesday, 3 September 2013

Traverese through Set with SetIterator - X++ AX 2012

Today we are going to go over the Set Class in AX 2012.

The MSDN definition is "The Set class is used for the storage and retrieval of data from a collection in which the values of the elements contained are unique and serve as the key values according to which the data is automatically ordered."

Scenario : We are going to Set with grids. So whenever the record is selected in a grid a record is added to the set. Set, Lists, and Maps are all similar, because they all are used for storage and retrieval of data. The benefit of using sets, is that we can avoid duplicate values in sets.

Further we are going to use the Set Iterator to traverse through the Set.

Since we are going to use a grid with a set let us start with the form's class declaration.

Prerequisites - Design a form with a grid and add any table you want as the data source and one button.

Step 1 Declare the Set and the Set Iterator

 public class FormRun extends ObjectRun  
 {  
   Set               DispatchSelectedRecords;  
   SetIterator       DispatchSelectedRecordsSI;  
   #define.blank('')  
 }  

Step 2 Initialize the Set

Override the Form's init method

 public void init()  
 {  
    ;  
    if (!element.args().menuItemName())  
    throw error("@SYS22539");  
    Dispatchselectedrecords = new Set(Types::Integer);  
    super();  
 }  

Now you might ask yourself how do we define what type of data will be adding to set. When initializing the set we have to set the type, DispatchSelectedRecords = new Set(Types::Integer);. There are set types for all of the available data types in AX 2012. 

In our case I have chosen to store integer values in my Set. Essentially these will be the Rec Id Values of the data source in my form grid.

Step 3 Override the MarkChanged Method on the Data Source.
 

 public void markChanged()  
 {  
   ;  
     if(Dispatchselectedrecords.in(TmpOutsourcedPOprocessingTable.RecId))  
       Dispatchselectedrecords.remove(TmpOutsourcedPOprocessingTable.RecId);  
     else  
       Dispatchselectedrecords.add(TmpOutsourcedPOprocessingTable.RecId);  

   super();  
 }  



In this method we are checking if the particular "RecId" is in the set and adding "RecIds" not in the set. We are basically checking for duplicates and adding the new items to the set.

Step 4 Traverse through the list.

Override the Clicked Method of the button your created

 void clicked()  
 {  
   OutsourcedPOprocessingTable OutsourcedPOprocessingTableLoc ;  
   TmpOutsourcedPOprocessingTable TmpOutsourcedPOprocessingTableloc ;  
   ;  
   Dispatchselectedrecordsse = new SetIterator(Dispatchselectedrecords);  
   ttsBegin ;  
   while (Dispatchselectedrecordsse.more())  
   {  
     TmpOutsourcedPOprocessingTableloc  = TmpOutsourcedPOprocessingTable::find(Dispatchselectedrecordsse.value(),TmpOutsourcedPOprocessingTable) ;  
     OutsourcedPOprocessingTableLoc    =  OutsourcedPOprocessingTable::find(TmpOutsourcedPOprocessingTableloc.BarCodeNo , TmpOutsourcedPOprocessingTableloc.TestCode , TmpOutsourcedPOprocessingTableloc.SalesId);  
     OutsourcedPOprocessingTableLoc.selectForUpdate(true);  
     OutsourcedPOprocessingTableLoc.DispatchStatus = NoYes::Yes ;  
     OutsourcedPOprocessingTableLoc.DispatchDateTime = DateTimeUtil::utcNow();  
     OutsourcedPOprocessingTableLoc.update();  
     if(Dispatchselectedrecordsse.more())  
     Dispatchselectedrecordsse.next();  
   }  
   ttsCommit ;  
   delete_from TmpOutsourcedPOprocessingTable ;  
   element.PopulateTable();  
   TmpOutsourcedPOprocessingTable_DS.executeQuery();  
   super();  
 }  


First initialize the SetIterator and pass the set in the Iterator. Then  use the "More" and "Next" Methods of the iterator to actually traverse though the Set.

In this method we are simply updating a table which has no bearing on the Set, but i am showing it to you because it aides in portraying a practical use of a set.



No comments:

Post a Comment