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

enum2str not working correctly

(0) ShareShare
ReportReport
Posted on by 1,552

Hi,

So i created a custom service, and when i try to call it from .net by doing the following:

DirPartyType = "Person" or DirPartType = "Per"

x code take it as person and my code enters the if statemenet that says dirPartyType::person. Why is that?

but if i try for example DirPartyType = "xx"  then i get invalid DirPartyType

Here's code in my service.

DirPartyType dirPartyType;
TestFactory::create(str2Enum(dirPartyType,_req.DirPartyType())


public static TestFactory create(DirPartyType _dirPartyType)
{
    if(_dirPartyType == DirPartyType::Person)
    {
        return new TestPCreator();
    }
    else if(_dirPartyType == DirPartyType::Organization)
    {
        return new TestOCreatorCreator();
    }
    else
    {
        throw error('Invalid DirPartyType');
    }
}

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,915 Super User 2025 Season 2 on at

    Hi Junior AX,

    Can you share where and how the service is validating or retrieving the party type value? The code you shared already has this value, I assume.

  • Verified answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    Hi Junior AX,

    As per the MS document for str2Enum function (docs.microsoft.com/.../aa655065(v=ax.60)  Retrieves the enum element whose localized Label property value matches the input string.

    You have to use something like this;

    class Community

    {

       /// <summary>

       /// Runs the class with the specified arguments.

       /// </summary>

       /// <param name = "_args">The specified arguments.</param>

       public static void main(Args _args)

       {

           DictEnum dictEnum = new DictEnum(enumNum(DirPartyType));

           dictEnum.symbol2Value('PER');

           //DirPartyType dirPartyType;        

           //Community::create(str2Enum(dirPartyType, "x"));

           Community::create(dictEnum.symbol2Value('PER'));

       }

       public static void create(DirPartyType _dirPartyType)

       {

           if(_dirPartyType == DirPartyType::Person)

           {

               //return new TestPCreator();

           }

           else if(_dirPartyType == DirPartyType::Organization)

           {

               //return new TestOCreatorCreator();

           }

           else

           {

               throw error('Invalid DirPartyType');

           }

       }

    }

  • junior AX Profile Picture
    1,552 on at

    Hi Faisal,

    It worked, but may you explain what is the difference between using DictEnum and what i originally did? and what does symbol2Value do?

    Hi Andre,

    in.Net i'm calling the service, by filling the contract parameters

    request.Customer = new customerContract()
    {
        CustGroupId = "group",
        TaxGroup = "tax",
        //DirPartyType = "Per"
        DirPartyType = "Person"
    };

    and as i mentioned in the code before, the value i filled in .Net is saved in _req.DirPartyType(), so i took this value and i passed it to TestFactory class

    DirPartyType dirPartyType;
    TestFactory::create(str2Enum(dirPartyType,_req.DirPartyType())

    and again here's the test factory class:

    public static TestFactory create(DirPartyType _dirPartyType)
    {
        if(_dirPartyType == DirPartyType::Person)
        {
            return new TestPCreator();
        }
        else if(_dirPartyType == DirPartyType::Organization)
        {
            return new TestOCreatorCreator();
        }
        else
        {
            throw error('Invalid DirPartyType');
        }
    }

  • junior AX Profile Picture
    1,552 on at

    Hi Faisal,

    I started getting this warning using your way:

    Warning Assigning a numerical value to extensible enum 'Extensible Enumeration(DirPartyType)' will yield unexpected results. Extensible enum values are not deterministically assigned to a numerical value.

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