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 :
Microsoft Dynamics AX (Archived)
Suggested Answer

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

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

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

  • Community Member Profile Picture
    on at

    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

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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.

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    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.

  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • Belgacem Afif Profile Picture
    17 on at

    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
    Rustem Galiamov Profile Picture
    8,072 on at

    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

  • Suggested answer
    Belgacem Afif Profile Picture
    17 on at

    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 :)

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans