Thursday, April 7, 2022

Resolving Business Central AL Warning AL0603 - Implicit conversion to a value of type Enum

 by Steve Endow

While copying a standard Business Central Web API page to a new project to create a customized version of the API page, I encountered this compiler warning:

AL0603 - Implicit Conversion to a value of type Enum

An implicit conversion is being performed from a value of type 'Integer' to a value of type 'Enum "Attachment Entity Buffer Document Type"'. This conversion can lead to unexpected runtime issues. This warning will become an error in a future release.AL AL0603
(field) "Document Type": Enum "Attachment Entity Buffer Document Type"

I am vaguely familiar with this error, and I think I resolved it months ago in a different Business Central project, but I completely forgot how I figured it out and how I resolved it.  So I'm writing this post to remind me when I forget again in 6 months.

I get the concept:  Business Central now supports Enums, so instead of referencing values as integers, you can reference them as Enum values.  Very simple in concept.

But if you look at the line of code with the warning, the value "Document Type" has the squiggly underline.  Since I am a complete newbie to AL, that squiggly line threw me off.  I looked at the "Document Type" value and thought I needed to replace it with something else.  Wrong.

If I would have read the warning message very, very, very carefully, I may have better understood the nature of the warning:

An implicit conversion is being performed from a value of type 'Integer' to a value of type 'Enum...'

FROM type Integer, TO type Enum

The value "Document Type" is presumably NOT an integer.  But const(1) does look like an integer.

Thanks to Jeremy Vyska for explaining the very simple fix to me.  I needed to replace the integer value of 1 with the corresponding Enum value.

But what exactly do I replace it with? How to I find the proper Enum value?  

Option 1

Fortunately my years of experience with Visual Studio and .NET development had some benefit, as I knew that I could click on a value and press F12 to "Go to definition" and drill into the value (you can also right click and select Go to definition).

So I clicked on "Document Type" to place the cursor on it, then pressed F12 to drill into the definition of the "Document Type" field.

F12 - Go To Definition of field name

That opens "Attachment Entity Buffer.dal" in a new tab.

Enum "Attachment Entity Buffer Document Type"

My interpretation of this page is that it shows a field called "Document Type", defined with an Enum data type.  It is that Enum data type that we need to investigate further.

So, click inside the Enum name and press F12 again.

F12 into Enum definition

Now we're talkin!

The Enum Data Type Definition for Attachment Entity Buffer Document Type

Here, we finally see the Enum Data Type Definition for Attachment Entity Buffer Document Type.  And we can now see that a value of 1 equates to the enum value "Journal".

Option 2

Daniel Rimmelzwaan suggested an alternative method of viewing Enum values that is simpler and quicker.

Find or create a "Trigger" section on the page, then use intellisense to navigate to the Enum data type and view the enum values.

In a trigger, type Enum:: to trigger intellisense

Now that we have found the Enum values, let's get back to Jeremy's instructions:  Replace the const int value of 1 with the value of Journal.

Replace integer value of 1 with enum value name

Notice that after deleting the number 1, as soon as you type "J", the intellisense displays a list of values.

With Enum values in place, the AL0603 warning goes away

No more AL0603 warning!

Notice that const(Journal) can be used, without quotes around Journal, but Journal Line, below, needs quotation marks because of the space in the name.  You can put quotes around Journal as well, but it is not required.

I suspected the solution was simple, and it is, but I just didn't know how to properly fix the warning.

Hope that was helpful!

Steve Endow is a Microsoft MVP in Los Angeles.  He works with Dynamics 365 Business Central and related technologies.

You can also find him on Twitter and YouTube, or through these links:

No comments:

Post a Comment

All comments must be reviewed and approved before being published. Your comment will not appear immediately.

How many digits can a Business Central Amount field actually support?

 by Steve Endow (If anyone has a technical explanation for the discrepancy between the Docs and the BC behavior, let me know!) On Sunday nig...