Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

How to see if str2Enum returns nothing

Posted on by Microsoft Employee

Hi

If I call str2enum, e.g like this,

NoYes ny;
ny = str2enum(ny, 'badvalue');

what value will ny get? I can see in the debugger that it does not have Yes, not No. It is just blank. But I have not found a working way to identify this in a if clause.

(!ny) does not work

(ny==null) does not work

Any ideas?

 

 

*This post is locked for comments

  • skaue Profile Picture
    skaue on at
    Re: How to see if str2Enum returns nothing

    So basically this works:

    static void Adtest_enum(Args _args)
    {
        NoYesCombo      n;
        ;
        if (!enum2str(str2Enum(n, 'bad')))
        {
            info('bad enum');
        }
    }
    
  • Suggested answer
    Re: How to see if str2Enum returns nothing

    Usually all enum values have nonblank labels (if not then it's not good at all as you cannot use a blank enum label in a query range) so you can call enum2str(ny) and check if it returns a blank string. If it does then the enum variable definitely holds an invalid value. Internally in case of an incorrect conversion AX assigns a value of 255 to an enum variable (in MorphX a maximum enum value that you can use is 250) but this is kind of... undocumented so enum2str() is a preferable way to check if a preceding str2enum() call returned a valid enum value.

  • somvir gahlawet Profile Picture
    somvir gahlawet 115 on at
    Re: How to see if str2Enum returns nothing

    Try this:

    static void Test(Args _args)
    {
      ABC  abc;
      str test;
        ;
       test = "A";
       if(str2Enum(abc,test) == ABC::A)
       {
         print "test";
         pause;

       }

    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: How to see if str2Enum returns nothing

    Hi Jonas,

    Are your intentions to just see if an enum exists?  If so use this global enum method instead of str2enum.

    static void Job35(Args _args)
    {

        NoYes ny;
        ;
        ny = enumName2Id("Baddy");
       
        if(ny > 0)
            info("Good enum");
        else
            info("Bad enum");

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans