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 :
Microsoft Dynamics AX (Archived)

Query with X++

(0) ShareShare
ReportReport
Posted on by

Hi 

I have written this query. And I want it to do following.

  1. Join SalesTable and SalesLine
  2. Join CustInvoiceTrans
  3. Print out SalesLines SalesID and NCAdditionalText
static void Job2(Args _args)
{
    Query                    query = new Query();
    QueryBuildDataSource     queryBuildDSSalesTable;
    QueryBuildDataSource     queryBuildDSSalesLine;
    QueryBuildDataSource     queryBuildDSCustInvoiceJour;
    QueryBuildRange              queryBuildRange;
    QueryRun                            queryRun;
    SalesLine                            salesLine;
    ;

    // Setup the primary datasource.
    queryBuildDSSalesTable  = query.addDataSource(tablenum(SalesTable));
    
    // Setup the secondary (joined) datasource.
    queryBuildDSSalesLine   = queryBuildDSSalesTable.addDataSource(tablenum(SalesLine));
    queryBuildDSSalesLine.joinMode(JoinMode::InnerJoin);
    queryBuildDSSalesLine.relations(true);
    
    queryBuildDSCustInvoiceJour   = queryBuildDSSalesTable.addDataSource(tablenum(CustInvoiceTrans));
    queryBuildDSCustInvoiceJour.addRange(fieldnum(CustInvoiceTrans,InvoiceDate)).value('30-09-2015');
    queryBuildDSCustInvoiceJour.joinMode(JoinMode::InnerJoin);
    queryBuildDSCustInvoiceJour.relations(true);
    queryRun = new QueryRun(query);
    while(queryRun.next())
    {
        salesLine   = queryRun.get(tablenum(SalesLine));
        info(strfmt('%1 %2',salesLine.SalesId,salesLine.NCAdditionalText ));
    }

}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    You forgot tell us what's the problem. Also I would recommend to check the generate SQL before trying to fetch data. It would help you to spot the missing relation.

    Here I have a few point for you (covering at least some bugs):

    • The join to SalesLine looks all right
    • Specifying joinMode(JoinMode::InnerJoin) isn't needed, because inner join is the default
    • There is no direct relation between SalesTable and InvoiceDate, therefore queryBuildDSCustInvoiceJour.relations(true) has no effect and the join ends up without any relation. Not only you'll get wrong data, but the result cartesian product will also waste plenty of resources. You'll need a relation over CustInvoiceSalesLink.
    • '30-09-2015' is an invalid range value format. Use queryValue() for this purpose, such as queryValue(mkDate(30,9,2015)).
  • Community Member Profile Picture
    on at

    Hi Martin

    I try this and I get empty lines

     Query                                    query = new Query();
        QueryBuildDataSource     queryBuildDSSalesTable;
        QueryBuildDataSource     queryBuildDSSalesLine;
        QueryBuildDataSource     queryBuildDSCustInvoiceJour;
        QueryBuildRange              queryBuildRange;
        QueryRun                            queryRun;
        SalesLine                            salesLine;
        SalesTable                           salesTable;
        CustInvoiceTrans                    custInvoiceTrans;
        ;
    
        // Setup the primary datasource.
        queryBuildDSSalesTable  = query.addDataSource(tablenum(salesTable));
        //queryBuildDSSalesTable.addRange(fieldnum(SalesTable,SalesId)).value('XXXXXX');
    
        // Setup the secondary (joined) datasource.
        queryBuildDSSalesLine   = queryBuildDSSalesTable.addDataSource(tablenum(salesLine));
        queryBuildDSSalesLine.addLink(fieldNum(salesTable,SalesId),fieldNum(salesLine,SalesId));
    
        queryBuildDSCustInvoiceJour   = queryBuildDSSalesTable.addDataSource(tablenum(custInvoiceTrans));
        queryBuildDSSalesLine.addLink(fieldNum(salesTable,SalesId),fieldNum(custInvoiceTrans,SalesId));
        queryBuildDSCustInvoiceJour.addRange(fieldnum(custInvoiceTrans,InvoiceDate)).value(queryValue(mkDate(30,9,2015)));
    
        queryRun = new QueryRun(query);
        
        
        while(queryRun.next())
        {
            salesLine   = queryRun.get(tablenum(SalesLine));
            info(query.toString());
        }


  • Community Member Profile Picture
    on at

    Sorry there should have been this in the last line.

    info(strfmt('%1 %2',salesLine.SalesId,salesLine.NCAdditionalText ));

  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    You write code constructing a query and then observe data returned by it, but you never look at the key part - the query itself. But that's exactly what you should be debugging...

    If you did that, you would find that your query is wrong:

    SELECT * FROM SalesTable(SalesTable_1)
    JOIN * FROM SalesLine(SalesLine_1)
    ON SalesTable.SalesId = SalesLine.SalesId
    AND SalesTable.SalesId = SalesLine.InventTransId JOIN * FROM CustInvoiceTrans(CustInvoiceTrans_1) ON ((InvoiceDate = {ts '2015-09-30 00:00:00.000'}))

    There is no link between SalesLine and CustInvoiceTrans and a weird extra link between SalesTable and SalesLine! Why? Because you call addLink() on the wrong datasource.

    Let me show you my refactored version of what you tried to write, returning the query string:

    Query query = new Query();
    QueryBuildDataSource salesTableDs;
    QueryBuildDataSource salesLineDs;
    QueryBuildDataSource invoiceTransDs;
    
    salesLineDs = query.addDataSource(tableNum(SalesLine));
    
    salesTableDs = salesLineDs.addDataSource(tableNum(SalesTable));
    salesTableDs.relations(true);
    
    invoiceTransDs = salesTableDs.addDataSource(tableNum(CustInvoiceTrans));
    invoiceTransDs.addLink(fieldNum(SalesTable, SalesId), fieldNum(CustInvoiceTrans, SalesId));
    invoiceTransDs.addRange(fieldNum(CustInvoiceTrans, InvoiceDate)).value(queryValue(mkDate(30,9,2015)));
    
    info(salesTableDs.toString());
  • Community Member Profile Picture
    on at

    Hi Martin

    Thanks alot for your help. The query seems to work fine, but I dont know why it does not return all the records that have an invoicedate of 23-09-2015. Is that because I use a while loop? 

     Query query = new Query();
        QueryBuildDataSource salesTableDs;
        QueryBuildDataSource salesLineDs;
        QueryBuildDataSource invoiceTransDs;
        QueryRun queryRun;
        SalesLine salesLine;
        CustInvoiceTrans customerInvoiceTrans;
        SalesTable salesTable;
     
        salesLineDs = query.addDataSource(tableNum(SalesLine)); 
        salesTableDs = salesLineDs.addDataSource(tableNum(SalesTable));
        salesTableDs.relations(true);
     
        invoiceTransDs = salesTableDs.addDataSource(tableNum(CustInvoiceTrans));
        invoiceTransDs.addLink(fieldNum(SalesTable, SalesId), fieldNum(CustInvoiceTrans, SalesId));
        invoiceTransDs.addRange(fieldNum(CustInvoiceTrans, InvoiceDate)).value(queryValue(mkDate(23,9,2015)));
        
        queryRun = new QueryRun(query);
      
       
     
       // info(salesTableDs.toString());
         while(queryRun.next())
        {
            salesLine   = queryRun.get(tablenum(SalesLine));
            customerInvoiceTrans   = queryRun.get(tablenum(CustInvoiceTrans));       
            info(strfmt('%1 %2',salesLine.SalesId,salesLine.Name));      
         
        }


  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    No, I'm pretty sure that "while" works exactly as it should. I don't know your data, but it's clearly filtered by your own query, namely by the joins. Most importantly, you won't see any orders that don't have any corresponding record in CustInvoiceTrans.

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans