AX / D365FO – How to show Query Builder Select syntax in an Infolog

To show the SQL command of a Query builder in an infolog just use the ToString() method like shown in code below Query q = new Query(); QueryBuildDataSource qbds,qbds1,qbds2; QueryBuildRange qbr; QueryRun qr; SalesLine salesLine = SalesLine::findInventTransId('xxxxxxx'); //InventTrans qbds = q.addDataSource(tableNum(InventTrans)); qbr = qbds.addRange(fieldNum(InventTrans, StatusIssue)); qbr.value(queryValue(StatusIssue::ReservPhysical)); //InventTransOrigin qbds1 = qbds.addDataSource(tableNum(InventTransOrigin)); qbds1.relations(true); qbds1.joinMode(JoinMode::InnerJoin); qbds1.fetchmode(QueryFetchMode::One2One); qbds1.addRange(fieldNum(InventTransOrigin, InventTransId)).value(salesLine.InventTransId); … Continue reading AX / D365FO – How to show Query Builder Select syntax in an Infolog

AX – D365FO – Count records in Query #d365fo

If your query has just one datasource you can use SysQuery::countTotal static void CountProjTableRecords(Args _args) { Query query = new Query(); QueryRun queryRun; QueryBuildDataSource qbd; qbd = query.addDataSource(tablenum(ProjTable)); queryRun = new QueryRun(query); info(strfmt("Total Records in Query %1", SysQuery::countTotal(queryRun))); // total records 1130 } If we need to count number of records of a query with … Continue reading AX – D365FO – Count records in Query #d365fo

AX – D365FO – Create RunbaseBatch Class with Query window selection

Step 1: Create a runbasebatch class with all the needed methods Step 2: Override the initParmDefault() method public void initParmDefault() { Query query = new Query(); QueryBuildRange queryBuildRange; QueryBuildDataSource queryBuildDataSource; query.addDataSource(tablenum(CustInvoiceJour)); queryBuildDataSource=query.addDataSource(tableNum(CustInvoiceJour)); //Add Query queryBuildRange=queryBuildDataSource.addRange(FieldNum(CustInvoiceJour,OrderAccount)); queryrun = new SysQueryRun(query); super(); } QueryRun queryrun() { return queryrun; } Step 3: In Run method you can apply your … Continue reading AX – D365FO – Create RunbaseBatch Class with Query window selection

AX – D365FO – Advanced filtering in code X++ on Query object

So, you are creating the query programmatically (of course using the Query* classes), and you need a statement like select * from tableA where columnB = val1 or columnC = val2 just do it like:     QueryBuildDataSource    qbds;     QueryBuildRange             qbr;    str                                      range;     // initializing the query and the datasource(s) here; will go like qbds = … Continue reading AX – D365FO – Advanced filtering in code X++ on Query object

D365FO – AX – Looping through table buffer (QueryRun) multiple times

queryRun queryRun;query query = new query(); CustTable custTable;query.addDataSource(tablenum(CustTable)); while (queryRun.next()){ custTable = queryRun.get(tablenum(CustTable)); // do something with the buffer ...} queryRun.reset() while (queryRun.next()) // another loop process{ custTable = queryRun.get(tablenum(CustTable)); // do something other with the buffer ...}