Get selected records in a grid on a form in D365FO

 Purpose:

The purpose of this document is to demonstrate how we can we Get selected records in a grid on a form  X++ based on a business logic.

Product:

Dynamics 365 for Finance and Operations.

Development approach:

Customization through CODE, we need to  get selected records in a grid on a form on clicked method to update values

Development:

Create a on clicked or clicked method to Get selected records in a grid on a form .



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);
            ttsbegin;
            tableLines.reason = "My Reason";
            tableLines.update();
            ttscommit;
            tableLines = selectionHelper.getNext();
            info(strFmt('Selected record.. %1',tableLines.myField));//Display selected record
        }
        MyLinesTable_DS.research();
        MyLinesTable_DS.refresh();
    }
}

Comments