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;

  //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;

Original post link

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.