Skip to main content

Notifications

Microsoft Dynamics AX forum
Suggested answer

how to make relations in views

Posted on by 55

Hi!
Some problems with relations in views (AX 2009), maybe someone can help me?

I have two SQL powered views 

  1. SalesView - data from SalesTable with join to CustTable
  2. SalesLineView - data from SalesLine with join to InventTable

and want to put them both in one form with relation. I mean if you click on SalesView record it updates grid with SalesLineView fields. 
In tables on form case, it's just need to set JoinSource property to right table name (e.g. SalesTable for SalesLine), but this logic don't work on views for me. 
Can it be done at all? 

  • Mea_ Profile Picture
    Mea_ 60,274 on at
    RE: how to make relations in views

    No, I did not. This line   " l = s.addDataSource(tableNum(pmi_salesline_view));" adds new data source, so, yes, you are adding it when you should not. ANd you should not do it in executeQuery, it should be done in init.

  • Patryk Mi Profile Picture
    Patryk Mi 55 on at
    RE: how to make relations in views

    You misunderstood me, I didn't add new datasource, I just said where i put this code.

    When I putting code in init method i have error:

    Error executing code: FormDataSource object not initialized.

    (C)\Classes\FormDataSource\Query

    (C)\Forms\Form3\Methods\init

    (C)\Classes\SysSetupFormRun\init - line 11

    Sorry for my lame question, I'm new in AX

  • Mea_ Profile Picture
    Mea_ 60,274 on at
    RE: how to make relations in views

    Why do you add new datasource? You need to add 2 data source in the form desginer, set join mode. They would not have auto relation created by AX, so in init method you will set it using addLink.  

  • Patryk Mi Profile Picture
    Patryk Mi 55 on at
    RE: how to make relations in views

    Hossein, maybe I am wrong, but I think that I'm using left join :)

  • Patryk Mi Profile Picture
    Patryk Mi 55 on at
    RE: how to make relations in views

    Thanks for answers guys! AddLink is that what i was looking for.

    Unfortunately, if i use this code:

    public void executeQuery()
    {
        QueryBuildDataSource s;
        QueryBuildDataSource l;
        Query query;
        ;
    
        query = pmi_sales_view_ds.Query();
        s = query.dataSourceTable(tableNum(pmi_sales_view));
        l = s.addDataSource(tableNum(pmi_salesline_view));
        l.joinMode(JoinMode::InnerJoin);
        l.fetchMode(QueryFetchMode::One2Many);
        l.relations(false);
        l.addLink(fieldNum(pmi_sales_view, SalesId), fieldNum(pmi_salesline_view, SalesId));
        info(query.xml());
    }


    it doesn't work, but i haven't any errors in log.

    I have put that code into Data Sources > PMI_salesline_view > Methods

    What I'm doing wrong?



  • Suggested answer
    Hossein.K Profile Picture
    Hossein.K 6,642 on at
    RE: how to make relations in views

    For customization,use this link:

    community.dynamics.com/.../how-joins-in-x-select-statement-are-translated-into-t-sql

  • Suggested answer
    Hossein.K Profile Picture
    Hossein.K 6,642 on at
    RE: how to make relations in views

    Hi Patryk Mi,

    AX does not do a right join. You can put your "right" table as top of the select statement, and do an outer join on the "left" table. This way it will return all rows as a SQL right join would for the right table, and only rows that match based on the relation from the left table.Results are identical to what you would expect with a right join.

  • Suggested answer
    Mea_ Profile Picture
    Mea_ 60,274 on at
    RE: how to make relations in views

    You cannot do it from form designer because it uses relations defined in AOT and you cannot have them for view but you can do it using x++ code, on init method using addLink method, please refer this thread for details

    community.dynamics.com/.../123239

Helpful resources

Quick Links

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,682 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,000 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,146

Featured topics

Product updates

Dynamics 365 release plans