Tip #67 | Convert Option field to Enum Field

So much to blog about. Insane. Our world has never changed so fast. Never.

Today I was working on our ForNAV AppSource App and I decided to clean up some of the warnings that were introduced with BC16.

This was about converting Options to Enums and vice versa.

Converting an Enum into an Option is easy. Just add ToInteger() and you’re done.

But how about the other way around?

An implicit conversion is being performed from a value of type 'Option' to a value of type 'Enum "Report Selection Usage"'. This conversion can lead to unexpected runtime issues. This warning will become an error in a future release.

First the question, why do you want to convert an Option in to an Enum. Just change your field to an Enum instead!

Well, in this case I depend on Microsoft who in their endless wisdom decided to change the Usage field in the Report Selections to an Enum but not the Warehouse Report Selections.

And we combine both in a Temporary table to have one Page for all report selections. Simplicity right?

The solution is not what you expect and I had to actually ask Esben and Michael for help.

The Microsoft Documentation is not very helpful.

It turns out that each Enum is a type. So you can write code like this:

with DefaultReportSelection do begin
  "Usage (Warehouse)" := "usage (warehouse)"::Receipt;
  Usage := "Report Selection Usage".FromInteger("Usage (Warehouse)");
  Sequence := '1';
  "Report ID" := FindReportID('Warehouse Receipt', '');
  Origin := 7355;
  Insert;
end;

You do not have to declare the Enum (in my case “Report Selection Usage”) as a variable. Intellisense also does not pick it up. Understandable because the list would be massive.

I hope this helps others running into this issue.

Comment List
Related
Recommended