Home

Sunday, June 26, 2016

Add Range in query ax 2012

static void AddRangeToQuery3Job(Args _args)
{
    Query q = new Query();  // Create a new query.
    QueryRun qr;
    CustTable ct;
    QueryBuildDataSource qbr1;
    str strTemp;
    ;

    // Add a single datasource.
    qbr1 = q.addDataSource(tablenum(CustTable));
    // Name the datasource 'Customer'.
    qbr1.name("Customer");

    // Create a range value that designates an "OR" query like:
    // customer.AccountNum == "4000" || Customer.CreditMax > 2500.

    // Add the range to the query data source.
    qbr1.addRange(fieldNum(CustTable, AccountNum)).value(
    strFmt('((%1.%2 == "US-012") || (%1.%3 > 100))',
        qbr1.name(),
        fieldStr(CustTable, AccountNum),
        fieldStr(CustTable, CreditMax)));

    // Print the data source.
    print qbr1.toString();
    info(qbr1.toString());

    // Run the query and print the results.
    qr = new QueryRun(q);

    while (qr.next())
    {
        if (qr.changedNo(1))
        {
            ct = qr.getNo(1);
            strTemp = strFmt("%1 , %2", ct.AccountNum, ct.CreditMax);
            //print strTemp;
            info(strTemp);
        }
    }
    pause;
}
================================================================
static void Job34(Args _args)
{

    Query q = new Query();  // Create a new query.
    QueryRun qr;
    InventTrans inventTrans;
    QueryBuildDataSource qbr1;
    str strTemp;
    ;

    // Add a single datasource.
    qbr1 = q.addDataSource(tablenum(InventTrans));
    // Name the datasource 'Customer'.
    qbr1.name("InventTrans");

    // Create a range value that designates an "OR" query like:
    // customer.AccountNum == "4000" || Customer.CreditMax > 2500.

    // Add the range to the query data source.
    qbr1.addRange(fieldNum(InventTrans, StatusReceipt)).value(
    strFmt('((%1 == %2 ") || (%1 == %3) ||  (%4 == %5)))',
        fieldStr(InventTrans, StatusReceipt),
        any2int(StatusReceipt::Purchased),
        any2int(StatusReceipt::Received),
        fieldStr(InventTrans, StatusIssue),
        any2int(StatusIssue::sold)));

    // Print the data source.
    print qbr1.toString();
    info(qbr1.toString());

    // Run the query and print the results.
    qr = new QueryRun(q);

    while (qr.next())
    {
        if (qr.changedNo(1))
        {
            inventTrans = qr.getNo(1);
            strTemp = strFmt("%1 , %2", inventTrans.ItemId, inventTrans.itemName());
            //print strTemp;
            info(strTemp);
        }
    }
    pause;
}
======================================================================
static void queryEmpDept(Args _args)

{
Query query;
QueryBuildDataSource queryBuildDataSource1,queryBuildDataSource2;
QueryBuildRange queryBuildRange;
QueryBuildLink queryBuildLink;
QueryRun queryRun;
CustTable deptTrainingTable;
CustTrans empTrainingTable;
;
// Create a new query object
query = new Query();
// Add the first data source to the query
queryBuildDataSource1 = query.addDataSource(tablenum(CustTable));
// Add the range to this first data source
queryBuildRange = queryBuildDataSource1.addRange(fieldnum(CustTable, AccountNum));
// Set the range
queryBuildRange.value("US-021");
// Add the second datasource to the first data source
queryBuildDataSource2 =queryBuildDataSource1.addDataSource(tablenum(CustTrans));
// Add the link from the child data source to the parent data
//source
queryBuildLink = queryBuildDataSource2.addLink(fieldnum(CustTable,AccountNum),fieldnum(CustTrans, OrderAccount));
// Create a new QueryRun object based on the query definition
queryRun = new QueryRun(query);
// Loop through all the records returned by the query
while (queryRun.next())
{
// Get the table data by using the get() method
deptTrainingTable = queryRun.get(tablenum(CustTable));
empTrainingTable = queryRun.get(tablenum(CustTrans));
info (strfmt("AccountNum %1,  Name %2, voucher Id %3, order Account %4 ", deptTrainingTable.AccountNum,deptTrainingTable.name(),empTrainingTable.voucher,empTrainingTable.AmountCur));
}
}

No comments:

Post a Comment