Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

How to set enum value without using enum type

(0) ShareShare
ReportReport
Posted on by 10

str2Enum(enumType,enumvalue)

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    Hi Martin,

    I did try to mark your suggestion as verified but I am getting error as "I dont have permission to update this reply"

  • Martin Dráb Profile Picture
    232,149 Most Valuable Professional on at
    RE: How to set enum value without using enum type

    If it answered you question, please use Did this answer your question? > Yes on the reply to verify it.

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    Hi Martin,

    Thanks you much for your help..

    Your solution made our day :)

  • Suggested answer
    Martin Dráb Profile Picture
    232,149 Most Valuable Professional on at
    RE: How to set enum value without using enum type

    If I understand it correctly, you have a table name, a field name and a string with the element name of an enum. And you want to get the numeric value of the enum element.

    The first thing to do is finding which enum is used by the field:

    SysDictField df = SysDictField::findFieldByName('HcmPersonPrivateDetails', 'Gender');
    EnumId enumId = df.enumId();

    With this knowledge, you can use SysDictEnum class to convert the symbol to a value:

    SysDictEnum dictEnum = new SysDictEnum(enumId);
    int value = dictEnum.symbol2Value('Male');

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    public static void runBulkUpdate()
        {
            MaskedValueConfiguration        maskedvalConfig;
            Query                           queryBase = new Query();
            Query                           queryRecord = new Query();
            QueryBuildDataSource            qbds;
            QueryBuildRange                 qbrRecord;
            QueryRun                        queryRun;
    
            qbds = queryBase.addDataSource(tableNum(MaskedValueConfiguration));
            queryRun = new QueryRun(queryBase);
    
            while(queryRun.next())
            {
                DictTable               dictTable;
                Query                   qr = new Query();
                QueryBuildDataSource    qbdsDocuParm;
                QueryRun                qRun;
                DocuParameters          docParm;
                Common                  commonBuffer;
    
                maskedvalConfig = queryRun.get(tableNum(MaskedValueConfiguration));
                dictTable = new DictTable(tablename2id(maskedvalConfig.TableName));
                qbdsDocuParm = qr.addDataSource(tablename2id(maskedvalConfig.TableName));
                qbdsDocuParm.addRange(fieldName2Id(tablename2id(maskedvalConfig.TableName),"DataAreaId")).value(curExt());
    
                qRun = new QueryRun(qr);
               
                commonBuffer = dictTable.makeRecord();
                commonBuffer.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
    
                select * from commonBuffer;
                  
                var uo = new SysDaUpdateObject(commonBuffer);
    
                uo.settingClause().add(maskedvalConfig.DataFieldName, new SysDaValueExpression(maskedvalConfig.DataFieldMaskedValue));
    
                ttsbegin;
                    new SysDaUpdateStatement().execute(uo);
                ttscommit;
            }
        }

    This is one of the scenario we are having problems with updating fields based of Enum.

    Please see our code as pasted above.

    Here the common buffer would have the table to which we want to perform bulk update.  In our case (HcmPersonPrivateDetails)

    And our target field to be updated in this is (Gender) based out of enum (HcmPersonGender).

    SO the syntax would be like as follows:

    uo.settingClause().add(maskedvalConfig.DataFieldName, new SysDaValueExpression(maskedvalConfig.DataFieldMaskedValue));

    now the SysDaValueExpression would be (SysDaValueExpression(maskedvalConfig."Male"))  // Pass enum (MALE) as string

    Now our actual problem is we cannot pass hardcode the enum type as it could be any thing or any type in real implementation.

    Hence we required your suggestion here how can we achieve this ?

  • Martin Dráb Profile Picture
    232,149 Most Valuable Professional on at
    RE: How to set enum value without using enum type

    All right, so the type is string. What is the value. Can you give us a concrete example, please?

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    Hi Martin,

    The value in maskedValConfig.DataFieldMaskedValue is a string.

    I mean if the field is based of an enum then since we can't pass it as the same type we are passing it as a string value.

    Because our custom table field is a string field.

    hence we tried to use the global conversion str2Enum,however we could not as we cannot pass the enum type dynamically..

    Please suggest if there is any possibility to achieve this.

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    Hi Martin,

    It will have a string value.

    I mean if a field is based of an enum lets say "HcmPersonGender" then the maskedValConfig.DataFieldMaskedValue would have "Male","Female" as string.  And this is where we are having problem that no value is being saved in such case.

    And hence we tried the conversion str2Enum() but unfortunately we could not as was unable to pass the enum type dynamically

    could you please provide some suggestion here?

  • D365_Wibes Profile Picture
    184 on at
    RE: How to set enum value without using enum type

    Hi Martin,

    If the field is based of an Enum.

    Lets say if its based of "HcmPersonGender" then we are passing value either "Male" or "Female".

    As you see we are passing the values as string thought the field is based of an enum and that's where we tried to use str2Enum() but we hanged with enum type as we cannot pass it dynamically..

  • Martin Dráb Profile Picture
    232,149 Most Valuable Professional on at
    RE: How to set enum value without using enum type

    What value do you have in your maskedvalConfig.DataFieldMaskedValue variable?

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,304 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans