Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Lookup control: show Name for the user but get ID in x++

Posted on by Microsoft Employee

Hello Everyone,

I create a lookup control. i displayed 2 fields (Id and Name) like the that : 

SysTableLookup            sysTableLookup;
    Query                          query;
    QueryBuildDataSource  qbds1;
    FormStringControl callerControl;
    ;
    sysTableLookup = SysTableLookup::newParameters(tablenum(UserInfo),this);
    query = new Query();
    qbds1 = query.addDataSource(tablenum(UserInfo));
    sysTableLookup.addLookupField(fieldNum(UserInfo, Id));
    sysTableLookup.addLookupField(fieldNum(UserInfo, Name),true);
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();

How can i get the Id value when i select one row from the lookup?

Remark : I can have many rows in the lookup with the same Name

exp : Id      |    Name

        123    |  "Text1"

        144    |  abc

         985   |  "Text1"

Thanks a lot :) 

*This post is locked for comments

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Lookup control: show Name for the user but get ID in x++

    Note that you don't have to set the second argument (true) in this case. If you don't specify it, the first field will be used, which is Id.

  • Suggested answer
    Belgacem Afif Profile Picture
    Belgacem Afif 15 on at
    RE: Lookup control: show Name for the user but get ID in x++

    Thanks for all,

    I should take the reponse of @Stephane M because i have the Id values a little significative

       sysTableLookup.addLookupField(fieldNum(UserInfo, Id), true);

       sysTableLookup.addLookupField(fieldNum(UserInfo, Name));

    but i'm not satisfied

    Thanks a lot everyone :)

  • Suggested answer
    Rustem Galiamov Profile Picture
    Rustem Galiamov 8,072 on at
    RE: Lookup control: show Name for the user but get ID in x++

    So, create two field. One is ID with lookup and second is field which will display user name of id from first field.

    To fill the second field you can use modified() method on first field.

    public boolean modified()
    {
        UserInfo ui;
        boolean  ret;
    
        ret = super();
    
        select ui
            where ui.id == this.valueStr();
    
        UserName.text(ui.name);
    
        return ret;
    }


    Screenshot-2018_2D00_10_2D00_23-at-11.29.12.png

  • Belgacem Afif Profile Picture
    Belgacem Afif 15 on at
    RE: Lookup control: show Name for the user but get ID in x++

    Thanks everyone for all your responses,

    I have tried with your suggestions but not correct result.

    For more details :

    My need is to show to users a list of Name and Id in lookup control and the user can select any row he want from this list.

    After that the user select a row, the Name of the selected row will be displayed in the lookup, then with the code x++ i need to get the Id of the selected row.

    I had tried with your responses but allways i get the Displayed Value (the Name value) ==> UserInfo_name.valueStr() : is the lookup control ==> from the code x++ allways this lookup control take the Name value not the Id value

    also, surly i shouldn't use the select statement like that (select Id, Name from UserInfo where Name=="UserInfo_name.valueStr()" )  because he can return 2 or more rows with the same Name and i need the specific row with the specific Id value

    Best regards.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Lookup control: show Name for the user but get ID in x++

    Hi Afif,

    In my opinion, you should use 

        sysTableLookup.addLookupField(fieldNum(UserInfo, Id), true);
        sysTableLookup.addLookupField(fieldNum(UserInfo, Name));

    so you lookup will show Id and Name but will return the Id.

    If you really want to hide the Id, I recommand you to use a form based lookup, with a grid conaining the 2 Fields, and then you can set the Field Id as "Visible : No".

    Greetz

  • Suggested answer
    Rustem Galiamov Profile Picture
    Rustem Galiamov 8,072 on at
    RE: Lookup control: show Name for the user but get ID in x++

    Hi Afif Belgacem!

    If you want to show in lookup only one field and then get the id which depend on selected value in this field you should leave only one row

    sysTableLookup.addLookupField(fieldNum(UserInfo, Name),true);

    then override modified() method on the control and write select statement using value from this control. But this is not a good solution.

    If you provide more information about your requirements, maybe we can suggest the better solution.

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: How can i get many values for the same row of lookup control ?

    You should use addLookupField(fieldNum(UserInfo, Name)) instead of addLookupField(fieldNum(UserInfo, Name), true). The second parameters says that you want to return this field, Name, which is exactly what you don't want.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can i get many values for the same row of lookup control ?

    Thanks for your response,

    I need to display the Name in the lookup and with the code x++ i need to select the Id value

    for that i added the two lignes

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How can i get many values for the same row of lookup control ?

    Try removing the second parameter "true" from this line:

    sysTableLookup.addLookupField(fieldNum(UserInfo, Name),true);

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans