Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Use AOT query in x++

(0) ShareShare
ReportReport
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

*This post is locked for comments

  • 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 230,379 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 230,379 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().

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,661 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,379 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans