Question
Answer and Explanation
To add two enums to a range in Dynamics 365 Finance and Operations (D365FO), you'll typically be working with query ranges or form controls that use enums. Here's how you can achieve this, along with explanations:
Scenario: Adding Enums to a Query Range
When you need to filter data based on multiple enum values in a query, you can use the `SysQuery::value()` method to specify the enum values. Here's an example:
Query query = new Query();
QueryBuildDataSource qbds = query.addDataSource(tableNum(MyTable));
QueryBuildRange qbr = qbds.addRange(fieldNum(MyTable, MyEnumField));
// Assuming MyEnum is the name of your enum
qbr.value(strFmt('(%1 == %2) || (%1 == %3)',
fieldStr(MyTable, MyEnumField),
SysQuery::value(MyEnum::EnumValue1),
SysQuery::value(MyEnum::EnumValue2)));
QueryRun qr = new QueryRun(query);
while (qr.next())
{
MyTable myTable = qr.get(tableNum(MyTable));
info(strFmt("Record with Enum Value: %1", myTable.MyEnumField));
}
Explanation:
1. Create a Query: We start by creating a new `Query` object and adding a data source for your table (`MyTable`).
2. Add a Range: We add a range to the query for the enum field (`MyEnumField`).
3. Specify Enum Values: The `qbr.value()` method is used to set the range criteria. We use `strFmt` to create a string that represents the condition: `(MyEnumField == EnumValue1) || (MyEnumField == EnumValue2)`. `SysQuery::value()` is used to get the correct string representation of the enum values.
4. Execute the Query: We then execute the query and iterate through the results.
Scenario: Adding Enums to a Form Control Range
If you're working with a form control that filters data based on an enum, you can use similar logic in the form's data source query. Here's an example of how you might do this in the `init` method of a form's data source:
public void init()
{
Query query;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
super();
query = this.query();
qbds = query.dataSourceTable(tableNum(MyTable));
qbr = qbds.addRange(fieldNum(MyTable, MyEnumField));
qbr.value(strFmt('(%1 == %2) || (%1 == %3)',
fieldStr(MyTable, MyEnumField),
SysQuery::value(MyEnum::EnumValue1),
SysQuery::value(MyEnum::EnumValue2)));
}
Explanation:
1. Get the Query: We get the query associated with the form's data source.
2. Add a Range: We add a range to the query for the enum field (`MyEnumField`).
3. Specify Enum Values: Similar to the previous example, we use `qbr.value()` with `strFmt` and `SysQuery::value()` to specify the enum values.
Important Considerations:
- Enum Names: Ensure you use the correct enum names and values.
- Performance: For large datasets, consider using a more efficient approach if you have many enum values to filter by. You might need to use a temporary table or a different query strategy.
- User Interface: If you need to allow users to select multiple enum values, you might need to create a custom form control or use a lookup field with a multi-select option.
By using these methods, you can effectively add two or more enum values to a range in D365FO, allowing you to filter data based on specific enum values.