Skip to main content

Notifications

Microsoft Dynamics AX forum
Suggested answer

How to join datasources with code?

Posted on by 920

I add 2 datasources in form , then I add this code in the init method of form

    
    
    parmid                  = element.args().parm();
    query = new Query();
    queryBuildDataSource = query.addDataSource(tableNum(LoadLicenseLevel));
    queryBuildDataSource.addRange(fieldnum(LoadLicenseLevel,ParentMenuItem)).value(parmid);

    queryBuildDataSource1 = queryBuildDataSource.addDataSource(tableNum(TreeNode));
    queryBuildDataSource1.addRange(fieldnum(TreeNode,MatrixRole)).value(MatrixID.valueStr());
    queryBuildDataSource1.joinMode(JoinMode::InnerJoin);
    queryBuildDataSource1.relations(false);
    queryBuildDataSource1.addLink(fieldNum(LoadLicenseLevel, EukBrowsedMenuItems), fieldNum(TreeNode, SecurableName));

But I don't get the exact values.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    RE: How to join datasources with code?

    All right then, Next time, please make sure you compile your code and fix compilation errors.

    Please show us your current code and configurations of data sources. Also, look at the actual query string and verify that you have at least one record meeting all criteria.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to join datasources with code?

    the name is EUKTreeNode , juste I write TreeNode ;)

    I add relation between tables but I don't get any values now

  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    RE: How to join datasources with code?

    Dynamics AX contains a class called TreeNode. And you're saying that you also have a table with the same name, TreeNode. It shouldn't be possible, because than the same type name would mean two different things. How could you compile and run code without knowing which of the two types you should use?

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to join datasources with code?

    I dont understand you

    If you really have two types with the same name ??

  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    RE: How to join datasources with code?

    If you really have two types with the same name (which I still struggle to believe), than your environment is a funny state and getting it fixed should be your top priority.

    If you don't have any relation and you want one, create it.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to join datasources with code?

    The table TreeNode (customized table) exists

    I don't find any relations between the 2 tables

    rel.png

  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    RE: How to join datasources with code?

    Unfortunately you still haven't describe your problem, so let me at least improve your design.

    First of all, there is no TreeNode table and - as far as I can say - none can exist, because the name is already taken by TreeNode class. Can you please confirm that you have such a table? If you don't and your code won't even compile, than you should obviously fix this bug.

    Also, you don't need any code to add data sources and set the type of join. You seem to already have form datasources; then simply set JoinSource and Link Type properties on the child data source.

    Regarding the join relation, consider if you can't add it to child table. If you can all, you don't need addLink() either. All code you'll need is setting query ranges on the existing data sources.

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to join datasources with code?

    Hi Martin,

    I want to display informations in Grid based on 2 datasources.:

    4540.grid.png

    in the 1rst column I  get BrowsedMenuItems from the 1rst datasource.LoadLicenseLevel

    2. Based on BrowsedMenuItems I want  to search these values in TreeNode datasources (Column SecurableName ).

    When I get the correct informations (BrowsedMenuItems exists into TreeNode in the column SecurableName);

    I want to display the  securityLevel from TreeNode datasource.

    in the init method I add this code that I send later.

  • Martin Dráb Profile Picture
    Martin Dráb 229,173 Most Valuable Professional on at
    RE: How to join datasources with code?

    Could you please provide a better description of your problem? "I don't get the exact values" can mean anything.

    Can you also show us how you're using the query? Because your code doesn't modify form's query - it creates a new independent query. If you don't explicitly set it to the form, all your code won't have any effect. But do you really want to create a new query instead of just filtering the automatically generated query? As usual, explaining what you're trying to achieve, instead of just showing a piece code, would help us to help you.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,867 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,173 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans