AX2012 / D365FnO – HOW TO CONVERT A STRING VALUE INTO AN ENUM VALUE USING X++

D365fno-PostImage

In this blog, I will try to explain how to convert string value into enum value in D365FnO X++.

If you are working with Dynamics 365 Finance and Operations (D365FnO), you may encounter situations where you need to convert a string value into an enum value in X++. For example, you may have a string value that represents a sales order status, and you want to assign it to a variable of type SalesStatus, which is an enum.
One way to do this is to use the str2Enum function, which takes two parameters the type of the enum and the string value to convert. The function returns the enum value that matches the string value, or 0 if no match is found.
 
Here is an example of how to use str2Enum to convert a string value into an enum value in D365FO X++. To begin, create a new runnable class and name it “StringToEnum“. Then, copy the code provided below into the newly created class.
 
class StringToEnum
{
    public static void main(Args _args)
    {
        // Declare a variable of type SalesStatus
        SalesStatus salesStatus;
        // Declare a string value that represents a sales order status
        str salesStatusValue = "Open order"; // ElementName : Backorder
        // Use str2Enum to convert the string value into an enum value
        salesStatus = str2Enum(SalesStatus, salesStatusValue);
        // Display the enum values
        info(strFmt("%1",salesStatus));
        info(enum2Str(salesStatus));
    }
}
The output of this code will be “Open order“, which is the name of the enum element that corresponds to the string value.
 
I hope this blog post helped you learn how to convert a string value into an enum value in D365FO X++.
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.