web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

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

  • Verified answer
    Martin Dráb Profile Picture
    240,742 Most Valuable Professional on at

    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().

  • AXT Profile Picture
    1,699 on at

    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
    240,742 Most Valuable Professional on at

    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
    1,699 on at

    Thanks, very good answer

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans