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, ...
Suggested Answer

how can I create custom query in x++ and store results in a view?

(0) ShareShare
ReportReport
Posted on by

Hello Everyone,

I am new to dynamics 365 and need your help. I want to retrieve specific records from one of the Dynamics 365 Fin-Ops tables. I want to use some conditions on the data hence; I'm trying it using x++ query instead of using drag-and-drop UI. 

Here's the scenario. I have order table where all the orders are being placed and I want to only retrieve orders which are recent under specific Id and componentId.

The SQL query is

"SELECT *

FROM OrderTable O

LEFT JOIN  OrderTable O2 ON O.Id = O2.Id AND O.ComponentId = O2.ComponentId

AND O.OrderDateTime <= O2.OrderDateTime

WHERE O2.LineId IS NULL"

I have tried GUI in visual studio by creating Query using Add Item > Query but not sure where I can above conditions. I know we can do it in x++ bu don't know how! Also want to store all these records in a view. Can anyone please guide me? 

I have the same question (0)
  • Suggested answer
    Naga Kiran Profile Picture
    on at

    Hi Harshil,

    To my understanding, you are trying to build a query based on join to same table. I am bit not sure of the business scenario for framing the query this way and sure there can be a better alternative for this scenario.

    How ever you can try the below example and please note that I have not tested this-

    Query  query = new Query();

    QueryBuildDataSource     queryOrderTable1;

    QueryBuildDataSource     queryOrderTable2;

    QueryBuildRange              queryBuildRange;

    QueryRun                          queryRun;

    OrderTable                         locOrdTable;

    queryOrderTable1 = query.addDataSource(tablenum(OrderTable));

    queryOrderTable2 = queryOrderTable1.addDataSource(tablenum(OrderTable));

    queryOrderTable2.addLink(fieldNum(OrderTable , Id), fieldNum(OrderTable , Id));

    queryOrderTable2.addLink(fieldNum(OrderTable , ComponentId ), fieldNum(OrderTable , ComponentId ));

    queryOrderTable2.joinmode(JoinMode::LeftJoin)

    queryOrderTable2.fetchMode(QueryFetchMode::One2One);

    queryOrderTable2.addRange(fieldnum(OrderTable,LineId )).value('0');

    queryRun = new QueryRun(query); //queryrun will have the resultset

    while(queryRun.next())
    {

    //play with the result set

    locOrdTable = queryRun.get(tablenum(OrderTable));

    }

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    You can't do exactly the same in F&O. For instance, F&O doesn't support NULL values. Therefore you must first think about a query that makes sense in the context of F&O.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi Harshil,

    You can create 2 views:

    1st view: Aggregated view with Max(OrderDateTime) with grouping by Id and ComponentId

    2nd view: select form OrderTable with existing join with view1 with relation by Id, ComponentId, and OrderDateTime

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
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans