Queries in AX


Queries are typically used to ask users about ranges and sorting, and then selecting
data based on the feedback from the users. A query can consist of one or multiple
data sources, and can be created both as static queries in the AOT or as dynamic
queries using X++.




static void queryRunRentalCarList(Args _args)
{
    Query                   query;
    QueryBuildDataSource    queryBuildDataSource1, queryBuildDataSource2;
    QueryBuildRange         queryBuildRange;
    QueryBuildLink          queryBuildLink;
    QueryRun                queryRun;
    CarTable                carTable;
    RentalTable             rentalTable;
    ;
    // Create a new query object
    query = new Query();
    // Add the first data source to the query
    queryBuildDataSource1 = query.addDataSource(tablenum(CarTable));
    // Add the range to this first data source
    queryBuildRange = queryBuildDataSource1.
    addRange(fieldnum(CarTable, ModelYear));
    // Set the range
    queryBuildRange.value("2008..");
    // Add the second datasource to the first data source
    queryBuildDataSource2 =
    queryBuildDataSource1.addDataSource(tablenum(RentalTable));
    // Add the link from the child data source to the parent data
    //source
    queryBuildLink = queryBuildDataSource2.addLink(
    fieldnum(CarTable,CarId),fieldnum(RentalTable, CarId));

    // 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
    carTable = queryRun.get(tablenum(CarTable));
    rentalTable = queryRun.get(tablenum(RentalTable));
    info (strfmt("CarId %1, RentalId %2", carTable.CarId,
    rentalTable.RentalId));
    }

}



No comments

Powered by Blogger.