15 October 2020

D365FO: mystery with handling multi selected grid rows from X++

Recently I had a simple task to handle multiple selected records in a form grid when a user clicks a form button. There is a special MultiSelectionHelper X++ class which is typically used in such scenarios. Here is a sample code:


public void clicked()
{
    super();

    YourTable yourTable;

    MultiSelectionHelper helper = MultiSelectionHelper::construct();
    
    helper.parmDatasource(YourTable_ds);

    yourTable = helper.getFirst();
    
    while (yourTable.RecId != 0)
    {
        info(int642Str(yourTable.RecId));

        yourTable = helper.getNext();
    }
}

In my case, however, the code above always displayed only one line in the Infolog, meaning that only one line was selected in the form grid.

After few hours of investigation the root cause of the problem was found. The form button had "Auto Refresh Data" property set to Yes. Call to the super(); methods was causing the grid to be refreshed, thus clearing up all multi selected lines and leaving only one selected lines.


The solution was to move super() call to the end of the method, allowing MultiSelectionHelper class to handle all selected lines properly.

Another mystery case is solved. Next time will be more careful with element properties and super() method calls.

No comments:

Post a Comment