Query execution using x++
In this article I will be going over how we can write a code based query to retrieve and filter data from the database without having to write a select statement.
For this example, I am going to extract the customer and sales order information for a specific customer.
Query query = new Query();
QueryBuildDataSource salesOrderQueryBuildDataSource = query.addDataSource(tableNum(SalesTable));
// add range for sales order with Id 1003
salesOrderQueryBuildDataSource.addRange(fieldNum(SalesTable, SalesId)).value(queryValue('1003'));
// join the customer table to retrieve the customer details
QueryBuildDataSource custTableQueryBuildDataSource =
salesOrderQueryBuildDataSource.addDataSource(tableNum(CustTable));
// add join type
custTableQueryBuildDataSource.joinMode(JoinMode::Exists);
// add join link
custTableQueryBuildDataSource.addLink(fieldNum(SalesTable, CustAccount), fieldNum(CustTable, AccountNum));
// initiate query execution
QueryRun queryRun = new QueryRun(query);
// execute query
while (queryRun.next())
{
// get the records for processing
SalesTable salesTable = queryRun.get(tableNum(SalesTable));
CustTable custTable = queryRun.get(tableNum(CustTable));
// infolog to display selected records
info(strFmt('Current record - Sales ID: %1, Customer Name: %2', salesTable.SalesId, custTable.name()));
}
Comments
Post a Comment