I'm Joris "Interface" de Gruyter. Welcome To My

Code Crib

Multi-select on form datasource grid : MultiSelectionHelper

Jan 7, 2013
Filed under: #daxmusings #bizapps

In Dynamics AX it has traditionally been somewhat cumbersome to iterate the selected records on a form datasource. The main issue is that there is a difference in iterating depending on how and what the user has selected!

For example, I have a simple form with a grid. The form has three buttons with their clicked method overwritten.

Dynamics AX allows you to iterate marked records on the grid. Assume our datasource is called “InventItemGroup”, which means we can access the object using “InventItemGroup_DS”. To iterate the marked records, you can call getFirst, and inside a loop call .getNext() until no more record is returned. The below code is the code behind door, erh, button number 1.

<pre>void clicked() { InventItemGroup itemGroup;

itemGroup = InventItemGroup_DS.getFirst(1);
while (itemGroup.RecId != 0)
{
    info(itemGroup.ItemGroupId);

    itemGroup = InventItemGroup_DS.getNext();
} }</pre></code>

Looks good. Unfortunately, this works ONLY when you actually MARK a record (in versions prior to AX 2012, when you click the little button in front of a row, in AX2012 when you click the checkbox). If you just select a line by clicking in one of the columns, the one record will not be picked up.

For example, this scenario results in, well, nothing.

These two examples below will result in the expected behavior (getting the item group in the infolog, that is).

As I’m sure you aware, you can also get the current record by just accessing the datasource directly. In the case of our example, InventItemGroup.

<pre>void clicked() { info(InventItemGroup.ItemGroupId); }</pre>

With one record selected, this obviously will work. So what happens with multiple records select? It will give you back the LAST record you clicked on. As an example, click one record, then hold the CTRL button on your keyboard pressed and select the next record. The code above will return the ItemGroupId of the last record you clicked on. If you reverse the order in which you clicked the records, you will see that.

So now, the easy way to fix this, is to combine both methods. This is “traditional” code as seen in all versions of Dynamics AX. If the getFirst() method does not return anything, take the datasource cursor instead.

But of course, there’s a better way in Dynamics AX 2012. A class called “MultiSelectionHelper” will do the heavy lifting for you. It does in fact do some extra checking as well, feel free to read the code behind it.

<pre>void clicked() { InventItemGroup itemGroup; MultiSelectionHelper helper = MultiSelectionHelper::construct();

helper.parmDatasource(InventItemGroup_DS);

itemGroup = helper.getFirst();
while (itemGroup.RecId != 0)
{
    info(itemGroup.ItemGroupId);

    itemGroup = helper.getNext();
} }</pre></code>

Happy coding. :-)

 

There is no comment section here, but I would love to hear your thoughts! Get in touch!

Blog Links

Blog Post Collections

Recent Posts