lundi 7 février 2011

How to use Queries in Dynamics AX

Create a query MyQuery as following :

























Then use The following job to see how to get data from this query

static void Queries_TestMyQuery(Args _args)
{
    SysQueryRun         queryRun = new SysQueryRun(querystr(MyQuery));
    CustInvoiceJour     custInvoiceJour;
    CustInvoiceTrans    custInvoiceTrans;
;

    if (queryRun.prompt())
    {
        while (queryRun.next())
        {
            custInvoiceJour  = queryRun.get(tableNum(CustInvoiceJour));
            custInvoiceTrans = queryRun.get(tableNum(CustInvoiceTrans));

            if (queryRun.changed(tableNum(CustInvoiceJour)))
            {
                info(strfmt("Account: %1", custInvoiceJour.invoiceAccount));
            }

            if (queryRun.changed(tableNum(CustInvoiceTrans)))
            {
                info(strfmt("Item: %1, Qty: %2, ",custInvoiceTrans.itemId, custInvoiceTrans.qty));
            }
        }
    }
}

You can get this data when selecting it with w join as in the following job:

static void Queries_MyQueryAsSelect(Args _args)
{
    CustInvoiceJour     custInvoiceJour;
    CustInvoiceTrans    custInvoiceTrans;
;

    while select custInvoiceJour
        join custInvoiceTrans
            where custInvoiceJour.salesId             == custInvoiceTrans.salesId
               && custInvoiceJour.invoiceId           == custInvoiceTrans.invoiceId
               && custInvoiceJour.invoiceDate         == custInvoiceTrans.invoiceDate
               && custInvoiceJour.numberSequenceGroup == custInvoiceTrans.numberSequenceGroup
    {
        // print fetch data
    }
}
Happy Daxing!

Aucun commentaire:

Enregistrer un commentaire