Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Use AOT query in x++

Posted on by 1,699

I created a query in the AOT. Now i want to use this query in x++ code but without writing the code for the datasource(s) incl. Joins (inner, outer), ranges, etc. again.

I was able to set the query:
query = new Query(queryStr(MyAOTQuery));

but i don't want to set the datasource again:
queryDs = query.dataSourceTable(tablenum(MyAOTTable));

because it's already defined in the AOT query. I'm just looking for a way to read the complete AOT query in x++ to use it.

Any ideas how to set this up?

Martin

  • AXT Profile Picture
    AXT 1,699 on at
    Re: Use AOT query in x++

    Thanks, very good answer

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,177 Most Valuable Professional on at
    Re: Use AOT query in x++

    OK, you could also call myTable = qr.getNo(1); to get the record by datasource index instead of table ID. But I prefer the first approach.

    myTable variable is necessary to cast Common (return type of both get()/getNo()) to your table, so you can use fields etc. It the same thing as if you have a method returning Object instance, but you know what the real type is so you cast that object to the proper type.

    If you don't need the casting (because you just want to send the record to another process or to save its RecId), you can avoid the myTable variable. But that's not a common case.

  • AXT Profile Picture
    AXT 1,699 on at
    Re: Use AOT query in x++

    Martin,

    Thanks for your answer. What i still do not understand is why i have to specify the table name:

    MyAOTTable myTable
    tableNum(MyAOTTable)

    When i create a new QueryRun instance i'm already referring to the query that exists in the AOT. So why AX does not know the datasource?

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,177 Most Valuable Professional on at
    Re: Use AOT query in x++

    It's not clear what you want to do with the query.
    You can simply create a QueryRun instance and run your query:

    QueryRun qr = new QueryRun(query);
    MyAOTTable myTable;
    while (qr.next())
    {
        myTable = qr.get(tableNum(MyAOTTable));
    }

    By the way, dataSourceTable() in your code doesn't create a new datasource, it reads the one you defined in AOT. You would add new datasource by calling addDataSource().

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

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

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans