Certainly! Enum fields in Dynamics 365 Finance and Operations (D365FO) are treated as Int64 data types. When comparing enum values, you need to use the enum2int
function to convert the enum value to its corresponding integer representation. Let's extend the previous code to demonstrate how to compare enum values using the DictField.baseType()
method:
static void FindFieldTypeAndCompareEnum(Args _args) { DictTable dictTable; DictField dictField; int fieldType; // Table name str tableName = 'CustTable'; // Field name str fieldName = 'CustClassification'; // Get the table dictionary dictTable = new DictTable(tableName2id(tableName)); if (dictTable) { // Get the field dictionary dictField = dictTable.fieldName2Id(fieldName); if (dictField) { // Get the field type fieldType = dictField.baseType(); // Display the field type info(strFmt("Field Type of %1.%2: %3", tableName, fieldName, fieldType)); // Check if the field is an Enum if (fieldType == Types::Enum) { CustClassification custClassification = CustClassification::High; // Convert the enum value to integer using enum2int int enumIntValue = enum2int(custClassification); // Retrieve the field's enum type str enumTypeName = dictField.typeName(); // Display the enum comparison result info(strFmt("Comparing %1.%2 with %3: %4", tableName, fieldName, custClassification, enumIntValue == enum2int(enumTypeName, dictField.enumType()))); } else { warning(strFmt("Field '%1' is not an Enum in table '%2'", fieldName, tableName)); } } else { warning(strFmt("Field '%1' not found in table '%2'", fieldName, tableName)); } } else { warning(strFmt("Table '%1' not found", tableName)); } }
Explanation:
enum2int: The
enum2int
function is used to convert an enum value to its corresponding integer representation.typeName: The
typeName
method of DictField returns the type name of the field, which is useful for obtaining information about the enum type.enum2int(enumTypeName, enumValue): This is used to convert an enum value to its integer representation based on the enum type name.
Comparing Enum Values: The code checks if the field type is an Enum and then compares an enum value (e.g.,
CustClassification::High
) with the stored value in the table.
Switch (dictField.baseType())
{
Case Type::Enum :
dictEnum = new DictEnum(dictField.enumID());
for (i = 0; i < dictEnum.values(); i++)
{
info (strfmt(“Enum value : %1“,dictEnum.value2Name(i)));
}
}
Usage:
Copy the extended code into the X++ editor in the Dynamics 365 Finance and Operations development environment.
Replace the values of
tableName
andfieldName
with the desired table and enum field names.Run the job, and the output will display the field type and the result of comparing the enum value with the stored value in the table.
This extended code provides a way to determine if a field is of enum type and how to compare enum values using the DictField.baseType()
method in D365FO X++.