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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to deal with Str2Enum

(0) ShareShare
ReportReport
Posted on by 351

Hi all,

I have a dynamically add excel column based on a data types.

Below code works good if its number. but if that enum element name getting error.

case Types::Enum:
if(str2numOK(columnVal.Value))
{
    Trans.(fieldName2id(Trans.TableId,columnsFields.AxFieldName)) = any2Int(columnVal.Value);
}
else
{
    Trans.(fieldName2id(Trans.TableId,columnsFields.AxFieldName)) = enum2Value(columnVal.Value);
}

As i mentioned above code is 'Else' part getting error:  

Function Global::enum2Value must be called with an enumerated text type.
How to deal with Str2Enum function give me a solution for this.
Thanks 
I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    Hi waytod365,

    As error stated that, you need to convert string to enum value before passing it to enum2Value method.

    Code should look like below.

    //declare variable for your enum.

    YourEnumName enum1;

    enum2Value(str2enum(enum1, columnVal.value));

    Thanks,

    Girish S

  • waytod365 Profile Picture
    351 on at

    Thanks for your prompt response Girish,

    Actually i wanna dynamically enum in the else part. I don't wanna specific enum variable. As you mentioned above code is dynamically? Can you tell the variable name?

    Thanks 

  • GirishS Profile Picture
    27,827 Moderator on at

    So in the excel you will have multiple enum elements. It may not be same enum for all the rows.

    In that case just use any2Enum method. 

    enum2::value(any2Enum(columnVal.Value));

    Thanks,

    Girish S.

  • waytod365 Profile Picture
    351 on at

    Hi Girish, i tried : enum2Value(any2Enum(columnVal.Value));

    When i import same error getting.

    Thanks

  • GirishS Profile Picture
    27,827 Moderator on at

    Add the any2Enum(columnVal.Value) to info log and check whether its returning any value.

    Also check the string thats coming from excel is a valid enum elements.

    Thanks,

    Girish S.

  • waytod365 Profile Picture
    351 on at

    The thing is same error and info log not come any value what could be the reason ?

    How to achieve this ?

  • GirishS Profile Picture
    27,827 Moderator on at

    What is the value you given in the excel? I mean what is the value for columnVal.value.

    Does the value exist in any of the enum elements?

    Thanks,

    Girish S.

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    If I understand it correctly, you need symbol2Enum(), not enum2Value(). Use DictField.enumId() to get the enum ID for the first argument:

    SysDictField dictField = SysDictField::newField(tableId2Name(trans.TableId), columnsFields.AxFieldName);
    trans.(dictField.id()) = symbol2Enum(dictField.enumId(), enumValue);

  • waytod365 Profile Picture
    351 on at

    Good catch :)

    Thanks for your answer martin sir, i didn't try your code yet, but where are u declared ""enumValue"

    Can you elaborate?

  • waytod365 Profile Picture
    351 on at

    Great sir , working as expected

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans