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 "TableName", which means we can access the object using "
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.
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.
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 "TableName", which means we can access the object using "
TableName
_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.void clicked()
{
TableName TableName_loc;
TableName_loc = TableName
_DS.getFirst(1);
while (TableName_loc
.RecId != 0)
{
info(TableName_loc
.ItemGroupId);
TableName_Loc= TableName_DS.getNext();
}
}
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.
void clicked()
{
info(InventItemGroup.ItemGroupId);
}
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.
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();
}
}
No comments:
Post a Comment