D365FOE. Issue with enums that have “Use Enum Value” property set to “No”.

Recently we have noticed incorrect behavior of enums that have “Use Enum Value” set to “No” and have gaps in enum values. These enums use values from properties when populated from user interface, however, X++ code uses generated values, that cause inconsistent behavior and data corruption.

To illustrate this issue, we will create new enum with two values: 0 and 10. Also we need to set “Use Enum Value” property to “No”.

MyEnum

MyEnumProperties

Zero:

MyEnumZero

Ten:

MyEnumTen

Also, we need simple table that has only one field, form to populate this field from UI and job to create data from X++.

Table:

MyTable

Form:

MyForm

Runnable class:

RunnableClass

Let’s run the class and check data in DB and UI.

In DB value is equal to value from enum properties – 10:

MyEnumInDBFromX++

On the form we can see empty value:

MyFormInsertFromX++

Let’s create new record using UI and check what is saved to DB.

MyFormInsertFromUI

MyEnumInDBFromUI

As you can see, value is equal to 1. For these enums value entered from UI will never be equal to value entered from X++ code, so if it is created in X++ user would see empty values, if it is created from UI X++ comparison like:

If (myTable.MyEnum == MyEnum::Ten)  

would always return false, even if user see “Ten” on UI, because values are different.

It was a common practice to have gaps in enum values for different layers, so you could avoid conflicts when new values are created in new version or a hotfix. AssetTransType is a good example, where localization related values start from 100, because they used to be on another layer.

However, most of standard enums use enum values, so probably that is why this bug was not spotted before.

As a good citizen, I filled a bug, but I have a sneaky suspicion that it won’t be fixed soon, so be aware of this volatile mix, try to avoid changing “Use Enum Value” property and review your enums in case you have one!

Advertisements