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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to add values on lookup based on another lookup?

(0) ShareShare
ReportReport
Posted on by 932

Hi,

I have 2 lookup.

in the first lookup I displayed the users.

in the second lookup I want to display only the roles assigned to the that user.

How can I display these roles in lookup ?

2210.role.png

*This post is locked for comments

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

    For the second field, you need to create a lookup method. There you will use a query, and then you must pass the value of your first field as a range for that query.

    Here you can see how to create a lookup method that uses a query: community.dynamics.com/.../ax-2012-create-lookup-in-x

  • BASMA Profile Picture
    932 on at

    I add this code  but I don't get values in the second lookup:

    public void lookup()
    
    {
    
       SecurityUserRole _SecurityUserRole;
    
       UserInfo _UserInfo;
    
       SecurityRole _SecurityRole;
    
       Query query;
    
       QueryBuildDataSource datasourceModule;
    
       QueryBuildDataSource datasourceLanguage;
    
       QueryBuildRange rangeElementType;
    
       QueryBuildRange rangeModuleId;
    
       SysTableLookup sysTableLookup;
    
       query = new Query();
    
       datasourceLanguage = query.addDataSource(tableNum(SecurityRole));
    
       datasourceModule = datasourceLanguage.addDataSource(tableNum(SecurityUserRole));
    
        select User from _SecurityUserRole
    
       join _UserInfo where _SecurityUserRole.User == _UserInfo.id && _UserInfo.name == UserInfo_name.Text()
    
       join Name, aotName ,Description from _SecurityRole where _SecurityRole.RecId == _SecurityUserRole.SecurityRole;
    
       datasourceModule.addRange(fieldNum(SecurityRole, Name)).value(queryValue(_SecurityRole.Name));
    
       sysTableLookup = SysTableLookup::newParameters(tableNum(SecurityRole), this);
    
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, Name));
    
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, aotName));
    
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, Description));
    
       //sysTableLookup.parmQuery(query);
    
       sysTableLookup.performFormLookup();
    
    }


  • AXTechie2120 Profile Picture
    568 on at

    Hi,

    Can you please explain why you are using the select statement,

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    1) Do you have any value in _SecurityRole.Name variable after the select statement is executed? If not, please re-evaluate your select statement.

    2) This select statement can return multiple records (one for each of the user's roles). At the moment you pass only the first role as a range to your query. Instead you should use a while select statement, and add a range for each _SecurityRole record that you get.

    3) You have commented out the line of code where you pass your query to the lookup ("//sysTableLookup.parmQuery(query);"). So your query has no impact on the lookup.

  • BASMA Profile Picture
    932 on at

    I use select satetement to get the roles from securityRole based on user selected on  the first lookup.

  • BASMA Profile Picture
    932 on at

    1.yes I get values after the select stetement

    2.I use while select statement

    3.I use sysTableLookup.parmQuery(query);

    but always I have the same issue. any values displayed in the 2 lookup

  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    For some reason you have added two data sources for your lookup, but you only need one.

    Also, I suggest using better names than "datasourceModule" and "datasourceLanguage".

    This code works and filters the lookup so that it shows only System administrator role. Now you just need to add your select statement to it and add your ranges based on the select statement:

    public void lookup()
    {
       Query query;
       QueryBuildDataSource qbds;
       SysTableLookup sysTableLookup;
    
       query = new Query();
       qbds = query.addDataSource(tableNum(SecurityRole));
       qbds.addRange(fieldNum(SecurityRole, Name)).value("System administrator");
       sysTableLookup = SysTableLookup::newParameters(tableNum(SecurityRole), this);
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, Name));
    
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, aotName));
    
       sysTableLookup.addLookupfield(fieldNum(SecurityRole, Description));
    
       sysTableLookup.parmQuery(query);
    
       sysTableLookup.performFormLookup();
    }


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

    Hi BASMA!

    Your code should looks like:

    public void lookup()
    {
        SecurityUserRole        securityUserRole;
        UserInfo                userInfo;
        SecurityRole            securityRole;
        Query                   query;
        QueryBuildDataSource    datasourceModule;
        QueryBuildDataSource    datasourceLanguage;
        SysTableLookup          sysTableLookup;
    
        
        select id from userInfo
            where userInfo.name == UserInfo_name.Text();  
            
        query = new Query();
    
        datasourceLanguage = query.addDataSource(tableNum(SecurityRole));
        datasourceModule   = datasourceLanguage.addDataSource(tableNum(SecurityUserRole));
        datasourceModule.addLink(fieldNum(SecurityRole, RecId), fieldNum(SecurityUserRole, SecurityRole));
        datasourceModule.addRange(fieldNum(SecurityUserRole, User)).value(queryValue(userInfo.id));
        
        sysTableLookup = SysTableLookup::newParameters(tableNum(SecurityRole), this);
        sysTableLookup.addLookupfield(fieldNum(SecurityRole, Name));
        sysTableLookup.addLookupfield(fieldNum(SecurityRole, aotName));
        sysTableLookup.addLookupfield(fieldNum(SecurityRole, Description));
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

    Hi Nikolaos, second lookup depends of user name.

  • Verified answer
    BASMA Profile Picture
    932 on at

    I modified the code and it works now :)

    public void lookup()

    {

       TableLookup = SysTableLookup::newParameters(tablenum(SecurityRole),this);

        query = new Query();

        qbds1 = query.addDataSource(tablenum(SecurityRole));

        qbds2 = qbds1.addDataSource(tablenum(SecurityUserRole));

        qbds2.joinMode(JoinMode::OuterJoin);

        qbds2.addlink(fieldNum(SecurityUserRole, SecurityRole), fieldNum(SecurityRole, RecId));

        while select User from _SecurityUserRole

        join _UserInfo where _SecurityUserRole.User == _UserInfo.id && _UserInfo.name == UserInfo_name.Text()

        join Name, aotName ,Description from _SecurityRole where _SecurityRole.RecId == _SecurityUserRole.SecurityRole

       {

       qds1.addRange(fieldnum(SecurityRole, Name)).value(queryValue(_SecurityRole.Name));

       }

       sysTableLookup.addLookupfield(fieldnum(SecurityRole, Name));

       sysTableLookup.addLookupfield(fieldnum(SecurityRole, AOTName));

       sysTableLookup.addLookupfield(fieldnum(SecurityRole, Description));

       sysTableLookup.parmQuery(query);

       sysTableLookup.performFormLookup();

    }

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

    Please use rich formatting and </> button to paste your code.

    And in your code you don't need to use while select if you join SecurityRole to SecurityUserRole by inner join.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans