Looping through a Forms Data Source in AX 2012

This will show you how to look through all of the records of a forms data source or just the selected records without affecting what is populated within the data source

How to loop through all records of a datasource with the name of InventTrans

InventTrans                 localInventTrans = InventTrans_DS.getFirst() as InventTrans;
ItemId                      itemId;

while(localInventTrans)
{
  //access fields by using localInventTrans.fieldname
  itemId = localInventTrans.ItemId;


  //get the next record from the datasource
        localInventTrans = InventTrans_DS.getNext() as InventTrans;
}
 

 How to loop through ONLY the selected records of a datasource with the name of InventTrans. This could be used if you have a grid and want the user to select only certain records then pass those records to a class, form, method, etc…


InventTrans                 localInventTrans;
ItemId                         itemId;

//get the first selected record of the datasource and loop through all of the selected records
for(localInventTrans = InventTrans_DS.getFirst(true) ? InventTrans_DS.getFirst(true) : InventTrans_DS.cursor(); localInventTrans; localInventTrans = InventTrans_DS.getNext())
{
                   itemId = localInventTrans.ItemId;
                   info(itemId);
}

Original post link
 https://community.dynamics.com/365/financeandoperations/b/ax2012codingaxclated/posts/looping-through-a-forms-datasource

Achieved my Requirement in AX 2012:
 i wanted to check the balance of selected records from a grid so using logic i solved my requirement.