Friday, July 27, 2012

Adding fields to the Batch (InventBatchId) lookup

Background

By default in Dynamics AX the lookup control for the inventory dimension Batch uses your item dimensions for the breakdown of inventory when providing a list of On Hand lots to choose from. However, we've re-used the Serial Number dimension (InventSerialId) as a sub dimension to break inventory down by and set it as both a Primary stocking and a Physical inventory dimension. Because of this we want to select an inventory dimension combination when choosing a lot that matches the values for all primary stocking and physical inventory dimension.

Problem

When viewing the lookup control there is an option to "Range inventory dimensions". If this is selected then the InventSerialId field will show in the On Hand grid, but it will only show rows that match the dimension value for the selected line. If the "Range inventory dimension" option is not selected then the lookup does not display the dimension in the grid and furthermore it does not consider the field in the group by fields, thereby aggregating all values together. Since our goal is to return the selected value to the caller form this is problematic because not only can users not select based on this field, the InventDim record returned has the field as blank.

Solution

The solution is two-fold. First we must set the dimension field to display in the grid and second we must make it as part of the group by field so that the values are returned to the caller.

The Batch lookup form is InventBatchIdLookup. This form uses the InventDimCtrl_Frm_Lookup class to control it's appearance and behavior. Our changes can be made to this class.

InventDimCtrl_Frm_Lookup.findGroupByDimensions() is the method which sets the visibility of fields in the grid. For my purposes adding the following line of code as the last line in the method makes the field visible.

dimParmVisibleGrid.(InventDim::dim2dimParm(fieldNum(InventDim, InventSerialId))) = NoYes::Yes;

InventDimCtrl_Frm_Lookup.executeInventDimQueryDatasource() is the method which controls the shape of the query used to display and return data in the lookup (the group by fields). Adding the following line of code to make InventSerialId a sort field to this method also puts the field in the group by.

qbdsDim.addSortField(fieldNum(InventDim, InventSerialId));