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 :
Finance | Project Operations, Human Resources, ...
Unanswered

how to add a new table to AOT query using code in this specific scenario?

(0) ShareShare
ReportReport
Posted on by 100

Hi I have specific scenario, suppose if there are 5 tables, Table1, Table2, Table3, Table4, Table5.

Table2 has a relation with Table1, Table3 has a relation with Table2, Table4 has a relation with Table3 and Table5 has a relation with Table4

I have an AOT query with Table3 as primary DS, Table4 as child DS of Table3 and Table5 as child DS of Table4

Now my query is if I want to add table1 and Table2 to existing query using code, how is that possible? Because if I get the qbds of Table3 or Table5 in code and add Table1 to it, its giving me wrong results compared to the new query I built.

This is what I am doing:

QueryBuildDataSource qbds5= query.dataSourceTable(tableNum(Table5));

QueryBuildDataSource qbds2 = qbds5.addDataSource(tableNum(Table2));

qbds2.addlink(Table3, RecId, Table2,RefRecId)

QueryBuildDataSource qbds1 = qbds2.addDataSource(tableNum(Table1));

qbds1.addlink(Table2, RecId, Table1,RefRecId)

or

QueryBuildDataSource qbds3= query.dataSourceTable(tableNum(Table3));

QueryBuildDataSource qbds2 = qbds3.addDataSource(tableNum(Table2));

qbds2.addlink(Table3, RecId, Table2,RefRecId)

QueryBuildDataSource qbds1 = qbds2.addDataSource(tableNum(Table1));

qbds2.addlink(Table2, RecId, Table1,RefRecId)

Both are giving wrong data, Can any experts suggest what is the best way to do this, or if it is at all possible to do this using code? I am aware that I can do this by making changes to the existing AOT query or create new qry but I am particularly looking to do this using code or see how it can be done.

(Only way I can see doing this with code is add Table4 again and then table3, table2, Table1)

I have the same question (0)
  • GirishS Profile Picture
    27,827 Moderator on at

    Hi JayVee13,

    If you have AOT query you can edit it and  add Table 1 as primary datasource and add all the other child tables.

    Also in the code you are adding a RecId as relation between tables. Thats wrong.

    Thanks,

    Girish S.

  • JayVee13 Profile Picture
    100 on at

    Hi Girish, Thanks for the reply, The RecIds are just for illustration of what I am doing but anyway it might be confusing so I have edited my post to avoid that by replacing with refRecId.

    Also I am particularly looking to do this using code as the AOT qry is being used in other places and I don't want my changes to effect those areas

  • GirishS Profile Picture
    27,827 Moderator on at

    Hi,

    So if you don't want to touch the existing query, you can create new AOT query and use it in your code. In that, you query can add Table1 as Primary table and join child tables to that.

    Thanks,

    Girish S.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 669 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 449 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 384 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans