29 June 2016

How to add enum filter with All element

Sometimes customers request a specific filter in a form. The filter is based on an enum and must have All element to display all records regardless of field value. For example, a filter has 3 elements: All, Quotation, Order; although enum has only 2 elements: QuotationOrder.

Problem description
Analyse how All element is added to an enum filter in standard AX.

Hints
To answer the question let's search for enums with All element and Filter suffix in standard AX.
Solution
There are mainly 2 approaches in standard AX:
- create duplicate enum with Filter suffix and additional All element
- create filter control in code.

Approach 1. Duplicate enum and add All element
Less code is needed to implement a filter, but more maintenance work is required when an enum is extended or changed. There are several examples in standard AX.

PayrollPeriodStatus and PayrollPeriodStatusFilter enums:
Note: elements of PayrollPeriodStatusFilter enum have different labels from PayrollPeriodStatus enum. It is user friendly, for example in PayrollCalculationFrequency form:

WrkCtrType and WrkCtrTypeFilter enums:
All element has no label, but the functional meaning behind it is the same. For example, in WrkCtrResourceAbilityMap form all Resource type are displayed when an empty element is selected in the filter:

Approach 2. Build ComboBox control in code
The algorithm is the following:
- an unbounded combobox control is added to a form
- All element and required base enum elements are added to the control during form initialisation
- internal map is used to store a relation between control elements and base enum elements.

An example in standard AX is StatusFilter control in WorkflowStatus form:
All element and base enum elements are added to the control in \Classes\WorkflowStatusForm\setRangeFilter method:
Note: In this case a map to store a relation between control and enum values is not created. It is not an optimal solution, because enum values can be out of range 0..(number of elements - 1) and the following code in WorkflowStatusForm.setStatzsFilter method may fail (marked in red):

There is another example in standard AX where a map is created:

Approach 3. Blank element
Based on the examples in standard AX I want to suggest an alternative solution - add the first enum element named Blank and without a label:
Use the enum as a filter in form. When Blank element is selected - all records are displayed, when another element is selected - specific range is applied.

Conclusion
Any of the mentioned approaches can be used to add enum filter with All element. It is more about a matter of taste.

No comments:

Post a Comment