To get selected record(s) in a grid in AX, we use the MultiSelectionHelper class. To achieve that, follow the steps below:
1. Create a command button on the form and override the clicked method.
2. Insert the code below:
void clicked()
{
MultiSelectionHelper selectionHelper = MultiSelectionHelper::construct();//Create instance of class
Set selectedRecords = new Set(Types::Record);//Declare a set of type record
MyLinesTable tableLines; //Your table buffer
super();
selectionHelper.parmDataSource(MyLinesTable_DS); //Set the datasource
tableLines = selectionHelper.getFirst(); //assign to table buffer the reference to selected record(s)
if (tableLines.RecId)
{
while (tableLines)
{
selectedRecords.add(tableLines);
info(strFmt('Selected record.. %1',tableLines.myField));//Display selected record
tableLines = selectionHelper.getNext();
}
}
}
3. Select record(s) on the grid then click on the command button to view the selected records.