Tuesday, February 12, 2013

Query by code in AX

Query Using Dyna Link

http://community.dynamics.com/product/ax/axtechnical/b/axaptavsme/archive/2012/07/17/building-a-query-object.aspx
static void CustTableSales(Args _args)
{
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildRange qbr1;
QueryBuildRange qbr2;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
qbds1 = query.addDataSource(tablenum(CustTable));
qbds1.addSortField(
fieldnum(CustTable, Name),
SortOrder::Ascending);
qbr1 = qbds1.addRange(fieldnum(CustTable,Blocked));
qbr1.value(queryvalue(CustVendorBlocked::No));
qbr2 = qbds1.addRange(fieldnum(CustTable,CustGroup));
qbr2.value(queryvalue(’10′));
qbds2 = qbds1.addDataSource(tablenum(SalesTable));
qbds2.relations(false);
qbds2.joinMode(JoinMode::ExistsJoin);
qbds2.addLink(
fieldnum(CustTable,AccountNum),
fieldnum(SalesTable,CustAccount));
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable = queryRun.get(tablenum(CustTable));
info(strfmt(
“%1 – %2″,
custTable.Name,
custTable.AccountNum));
}
}

Query Using without Dyna Link

static void Query_Example(Args _args)
{
    Query q;
    Queryrun qr;
    QueryBuildRange qbr;
    QueryBuildDataSource qbds;
    InventTrans iv;
    Real Total;
    str range;
    
    /* The following query produces the same results as:
    while select sum(qty) from inventTrans
        where (inventtrans.ItemId == "OL-1500") || inventtrans.ItemId == "OL-1000"
            join inventDim
                group by inventBatchId
                where inventDim.InventDimId == inventTrans.InventDimId */

    // Instantiate the query class.
    q = new query("Inventory_Transactions"); 
   
    // Create a data source by using the InventTrans table.
    qbds = q.addDataSource(tablenum(InventTrans));
 
    // Select only the Qty field, and then sum the Qty field.
    qbds.addSelectionField(fieldnum(InventTrans,Qty),selectionfield::Sum); 

    // Set the range to the ItemId field.
    qbr = qbds.addRange(fieldnum(InventTrans,ItemId));  

    // The range for the where statement specifies an 'or' statement.
    qbr.value(strfmt('((%1 == "%2") || (%1 == "%3"))',fieldstr(inventtrans,ItemId),'OL-1500','OL-1000'));

    // The following is the alternative way to enter the range.
    // This also limits the selection on the ItemId values.
    range = strfmt('((ItemId == "%1")||(ItemID =="%2"))',queryvalue('OL-1500'),queryvalue('OL-1000'));
    qbr.value(range);    

    // Create the join to the InventDim table.
    qbds = qbds.addDataSource(tablenum(InventDim));
  
    // Specify the table relationship.
    qbds.relations(true);
  
    // Indicate the order mode as the grouping.
    qbds.orderMode(ordermode::GroupBy);
 
    // Specify the grouping on the InventBatchId field.
    qbds.addSortField(fieldnum(InventDim,InventBatchId)); 

    // Instantiate the QueryRun class for the form.
    qr = new QueryRun(q);  

    // If the user clicks OK, continue.
    if (qr.prompt())  
    {

        // While there are records in the query, continue.
        while (qr.next())  
        {
            
           // Set the value of the query to the iv table. 
           bufferiv = qr.get(tablenum(InventTrans)); 

            // Create the value of the total field.
           total =  iv.Qty;  
        }
    }
    // Specify the quantity for the item.
    info(strfmt("Quantity: %1",total));  
    // Indicate the SQL string that is used for the query.
     info (qr.query().dataSourceNo(1).toString());  
}

No comments:

Post a Comment