Working with Data in Forms - Processing multiple
records
Note: This tutorial works on all Ax versions, the example that I made was on AX 2009.
we are going to explore several ways of achieving this goal. We will modify
the Items form in the Inventory management module
by adding a new button to it, which lists currently selected records in the
overview grid.
How to do it....
void
processSelectedItems()
{
InventTable
inventTableLocal;
;
for (inventTableLocal = InventTable_ds.getFirst(true)
?
InventTable_ds.getFirst(true)
:
InventTable;
inventTableLocal;
inventTableLocal =
InventTable_ds.getNext())
{
info(strfmt( "You've selected item '%1'", inventTableLocal.ItemId));
}
}
Property
|
Value
|
Name
|
ProcessSelectedItems
|
Text
|
Process
|
MultiSelect
|
Yes
|
void clicked()
{ ;
super();
element.processSelectedItems();
}
4. To test the record selection, open Inventory
management | Item Details, select several records
using SHIFTor CTRL and click the Process button. The selected items should be displayed in
the Infolog:
How it works...
The key element in this recipe is a for statement in processSelectedItems(). First, it checks if more than one record is selected
by calling getFirst(true) on the InventTable form data source. If yes, the for loops
all the selected records from the data source, otherwise it uses the cursor,
which corresponds to the currently selected single record. All selected records
are then stored in the local variable inventTableLocal.
In this example, we just output inventory item numbers into the Infolog using the global info() function.
The ProcessSelectedItems button is used to call the function above once the user clicks it.
Notice that its property MultiSelect is set to Yes to ensure it is still enabled when multiple
records are selected.
I have also experienced that sometimes Dynamics AX users struggle to select
multiple records using SHIFTor CTRL.
Sometimes, it is not clear that the form itself supports multiple record
processing. In such cases a more convenient way of selecting multiple records
could be to add a new checkbox in front of each record. This would remove all
confusion and improve user experience. In order to implement that, we need to
make few changes to the example above.
Map marked;
marked = new Map(Types::Int64, Types::String);
marked = new Map(Types::Int64, Types::String);
edit boolean editMark( boolean _set, InventTable _inventTable, boolean _mark)
{ ;
if (_set)
{
if (!_mark)
{
if (marked.exists(_inventTable.RecId))
{
marked.remove(_inventTable.RecId);
}
}
else
{
marked.insert(_inventTable.RecId, _inventTable.ItemId);
}
}
return marked.exists(_inventTable.RecId);
}
We also add a new CheckBox to the top of the form's Grid in the Overview tab page with the following properties:
Property
|
Value
|
Name
|
Mark
|
Label
|
Select
|
DataSource
|
InventTable
|
DataMethod
|
editMark
|
Finally, we have to modify processSelectedItems() to
loop though the map. Replace the method with the following code:
void processSelectedItems()
{
MapEnumerator mapEnumerator;
;
mapEnumerator = marked.getEnumerator();
while (mapEnumerator.moveNext())
{
info(strfmt("You've selected item '%1'", marked.lookup(mapEnumerator.currentKey())));
}
}
Open the Items form again and notice that now it has a new checkbox Select in front of
each record. Pick several records using it and click Process. The selected items should be displayed in the Infolog:
The principle of this technique is that we use a Map type
object to store the list of selected item numbers. The editMarked() method is bound to the checkbox control and is
responsible for adding a record to the map upon user selection and removing it
from the map if the user deselects the checkbox.
Aucun commentaire:
Enregistrer un commentaire