web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

how to validate string values for updating fields of type enum

(0) ShareShare
ReportReport
Posted on by 2,180

Hello

I am creating a job which goal is to update certain fields of a table ( via recid)

2018_2D00_07_2D00_08_5F00_11_2D00_15_2D00_58.jpg

I am scanning a CSV file and before doing any update , I want to include a validation for each column and values.

If the values are strings this is fine, however when it comes to fields which are enum type or numeric I don't know how to validate  properly. 

Let me explain better

For example , column 3 has    0 or 1 as possible   values  ,

I  want the script to throw an error if the value  ( from the file ) is not 0 or 1 and it is coherent with the enum type ( WorkTimeControl 0 - open , 1 -closed)

This is how I have declared the variable

2018_2D00_07_2D00_08_5F00_11_2D00_20_2D00_01.jpg

2018_2D00_07_2D00_08_5F00_11_2D00_20_2D00_48.jpg

however , even if in the file is 0 , the code is returning true in the condition below and throwing the error

if(str2enum(322,WorkTimeControl) != 0 || str2enum(322,WorkTimeControl) != 1 )

{
INFO(STRFMT("Value accepted are only 0 and 1,the value is %1 instead.Update will be stopped. LineRecId %2",WorkTimeControl,LinesRecId));
k++;
c = csvFile.read();
break;

}

What I am doing wrong?

*This post is locked for comments

I have the same question (0)
  • Anton Venter Profile Picture
    20,301 Super User 2025 Season 2 on at
    RE: how to validate string values for updating fields of type enum

    The function str2enum expects you to pass the enum label (Open, Closed etc.) instead of the value (0, 1 etc.).

  • Aparisi82 Profile Picture
    2,180 on at
    RE: how to validate string values for updating fields of type enum

    Based on what you suggested I created the script

    static void Job20(Args _args)

    {

     WorkTimeControl WorkTimeControl;

     str 10   WorkTimeControlValue = 'Open' ;

      ;

     info(strfmt("%1",str2enum(WorkTimeControl,WorkTimeControlValue)));

    }

    This job returns the value 'open' which I assume it is a string value however from Microsoft says that it returns an integer

    in my script I did

    str 10                                         WorkTimeControlValue;

    WorkTimeControl                      WorkTimeControl;

    WorkTimeControlValue             = conpeek(c,3);   ( and the value is 'open' from the file )

    however AX says that the  Operand types are not compatible with operator

       if(str2enum(WorkTimeControl,WorkTimeControlValue) != 'open'  ||str2enum(WorkTimeControl,WorkTimeControlValue) != 1 )

               {

                INFO(STRFMT("Value accepted are only 0 and 1,the value is %1 instead.Update will be stopped. LineRecId %2",WorkTimeControl,LinesRecId));

                k++;

                c = csvFile.read();

                break;

               }

    if I apply the following , the code is fine however it will pass true   because it says  open != 0

              if(str2enum(WorkTimeControl,WorkTimeControlValue) != 0 ||str2enum(WorkTimeControl,WorkTimeControlValue) != 1 )

               {

                INFO(STRFMT("Value accepted are only 0 and 1,the value is %1 instead.Update will be stopped. LineRecId %2",WorkTimeControl,LinesRecId));

                k++;

                c = csvFile.read();

                break;

    Any clue?

  • Verified answer
    Anton Venter Profile Picture
    20,301 Super User 2025 Season 2 on at
    RE: how to validate string values for updating fields of type enum

    The str2enum function returns the enum value which is an integer and not a string, that's why it's working in the second section of code:

    if(str2enum(WorkTimeControl,WorkTimeControlValue) != 0 || str2enum(WorkTimeControl,WorkTimeControlValue) != 1)

    The statement is a || (OR) statement instead of a && (AND) statement. This translates to: "if not 0 or not 1", so it will always be true because the value is either 1 or 0.

    Change your statement to:

    if(str2enum(WorkTimeControl,WorkTimeControlValue) != 0 && str2enum(WorkTimeControl,WorkTimeControlValue) != 1)

    This translates to "if not 0 and not 1".

  • André Arnaud de Calavon Profile Picture
    299,576 Super User 2025 Season 2 on at
    RE: how to validate string values for updating fields of type enum

    Hi Alessandro,

    The next job is an example how to validate an enum value regardless if you provide a numeric or text value as input:

    static void validate(Args _args)
    {
        #define.EnumValue('EnumValue')
    
        str 10                  workTimeControlValue = '0';
        WorkTimeControl         workTimeControl; 
        
        SysDictEnum             dictEnum;
        TreeNode                enumElements;
        TreeNodeIterator        enumIterator;
        
        int                     enumValue;
        int                     enumValueOrig;
        int                     errConst = 255;
        container               con;
        
           
        int string2Enum(str _str, EnumId _enumId)
        {
            SysDictEnum dictEnumLocal;
            int         enumValueLocal;
            #define.numerals('0123456789')
    
            dictEnumLocal = new SysDictEnum(_enumId);
    
            if (strKeep(_str,#numerals) == _str)
            {
                enumValueLocal = str2int(_str);
            }
            else
            {
                if ((dictEnumLocal.name2Value(_str) != 255))
                {
                    enumValueLocal = dictEnumLocal.name2Value(_str);
                }
                else
                {
                    enumValueLocal = dictEnumLocal.symbol2Value(_str);
                }
            }
    
            return enumValueLocal;
        }
        
       
        
        dictEnum = new SysDictEnum(enumNum(workTimeControl));
        
        enumValue = string2Enum(workTimeControlValue, enumNum(workTimeControl));
        enumValueOrig = enumValue;
        if(enumValue != errConst)
        {
            if (enumValue > (dictEnum.treeNode().AOTchildNodeCount() - 1))
            {
                enumValue = errConst;
            }
            enumElements = dictEnum.treeNode().AOTfirstChild();
            enumIterator = dictEnum.treeNode().AOTiterator();
            if(enumElements.AOTgetProperties())
            {
                while(enumIterator.next())
                {
                    con += enumElements.AOTgetProperty(#EnumValue);
                    enumElements = enumElements.AOTnextSibling();
                }
                if ((conFind(con, int2str(enumValueOrig)) != 0))
                {
                    enumValue = enumValueOrig;
                }
                else
                {
                    enumValue = errConst;
                }
                con = conNull();
            }
    
        }   
        
        if (enumValue == errConst)
        {
            error(strFmt("Value accepted are only 0 and 1,the value is %1 instead.Update will be stopped. LineRecId %2", workTimeControlValue /*, lineNum*/));
        }
        else 
        {
            info("correct value");
        }
    
        
    }

    I copied it from an example I had before. I tried to match it a bit with your example. You can improve it for your scenario. E.g. make it more flexible and move the local method to a separate class method.

  • Aparisi82 Profile Picture
    2,180 on at
    RE: how to validate string values for updating fields of type enum

    .... you are right... I  got confused with basic development syntax.. :)

  • Aparisi82 Profile Picture
    2,180 on at
    RE: how to validate string values for updating fields of type enum

    I will try to study this.

    Thank you very much.

  • Anton Venter Profile Picture
    20,301 Super User 2025 Season 2 on at
    RE: how to validate string values for updating fields of type enum

    You are welcome :-)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Community Member Profile Picture

Community Member 2

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans