Hello, community.
I'm trying to create a query request but have some trouble.
I created a request. Added a QueryBuildDataSource with parameters and return and try to execute it on and my result differs than I expected to see.
My code below.
VendAccount partnerCode; partnerCode = "V-001, V002"; my_List = strSplit(partnerCode, ','); my_listEnumerator = my_list.getEnumerator(); while (my_listEnumerator.moveNext()) { //init queryRun Query query = new query(); QueryBuildDatasource qbds; QueryRun queryRun; ; qbds = query.addDataSource(tablenum(VendPackingSlipJour)); qbds.firstOnly(false); qbds.firstFast(false); queryRun = new QueryRun(query); //Build query qbds = queryrun.query().dataSourceTable(tablenum(VendPackingSlipJour)); qbds.addSortField(fieldnum(VendPackingSlipJour, PurchId)); qbds.addSortField(fieldnum(VendPackingSlipJour, createdDateTime)); if (fromDate != dateNull() || toDate != dateNull()) { qbds.addRange(fieldNum(VendPackingSlipJour, createdDateTime)).value(SysQuery::range(fromDate, toDate)); } qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(SysQuery::value(my_listEnumerator.current())); //Build a report while(queryRun.next()) { queryRun.get(tableNum(VendPackingSlipJour); //output data to report } }
NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001') OR (InvoiceAccount = N' V-002')) TYPE: QueryBuildDataSource
SELECT * FROM VendPackingSlipJour WHERE ((createdDateTime >= '2020-03-31T17:00:00' AND createdDateTime <= '2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001') OR (InvoiceAccount = N'V-002')) ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC;
This code fills a report only for V-001.
Why this code didn't work for V-002
I built a query on SQL management studio and query works for V-001 and V-002.
Please help to understand why this occurs on AX2009.
Hi Anton,
There is a space char in second invoiceAccount filer, try to trim value with strLRTrim before applying it.
InvoiceAccount = N'[spaceV-002'
qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(SysQuery::value(strLRTrim(my_listEnumerator.current())));
The filter returns the result only for V-001.
No difference between these two constructions
NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001')) TYPE: QueryBuildDataSourceand this
NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001') OR (InvoiceAccount = N' V-002')) TYPE: QueryBuildDataSourceThe AX2009 returns are the same results.
Do I need to use constructions select VendPackingSlipJour where... or while select VendPackingSlipJour where... for achieving my goals?
Hi Anton,
Does filter by on vendor account in the loop return more records?
Hello Segei,
I had tryed to build a query on AX2009 query parameters with ((InvoiceAccount = N'V-001') OR (InvoiceAccount = N' V-002')) a few days ago. My QueryBuildDatasource looks like
NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001') OR (InvoiceAccount = N' V-002')) TYPE: QueryBuildDataSource
This query returns the result only for the first InvoiceAccount. But on SQL Management studio returns the result for both InvoiceAccounts.
Therefore I decided to rewrite the code and rose 2 simple queries like
Query1 NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-001')) TYPE: QueryBuildDataSource Query2 NAME: qbds VALUE: SELECT * FROM VendPackingSlipJour ORDER BY VendPackingSlipJour.PurchId ASC, VendPackingSlipJour.createdDateTime ASC WHERE ((createdDateTime>='2020-03-31T17:00:00' AND createdDateTime<='2020-04-29T17:00:00')) AND ((InvoiceAccount = N'V-002')) TYPE: QueryBuildDataSource
Hi Anton,
Your code doesn't match to query you provided.
You create a new query for each value from my_listEnumerator. If you want to get "OR" in your query
VendAccount partnerCode; my_List = strSplit(partnerCode, ','); my_listEnumerator = my_list.getEnumerator(); //init queryRun Query query = new query(); QueryBuildDatasource qbds; QueryRun queryRun; ; partnerCode = "V-001, V002"; qbds = query.addDataSource(tablenum(VendPackingSlipJour)); qbds.firstOnly(false); qbds.firstFast(false); queryRun = new QueryRun(query); //Build query qbds = queryrun.query().dataSourceTable(tablenum(VendPackingSlipJour)); qbds.addSortField(fieldnum(VendPackingSlipJour, PurchId)); qbds.addSortField(fieldnum(VendPackingSlipJour, createdDateTime)); if (fromDate != dateNull() || toDate != dateNull()) { qbds.addRange(fieldNum(VendPackingSlipJour, createdDateTime)).value(SysQuery::range(fromDate, toDate)); } while (my_listEnumerator.moveNext()) { qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(SysQuery::value(strLRTrim(my_listEnumerator.current()))); } //Build a report while(queryRun.next()) { queryRun.get(tableNum(VendPackingSlipJour); //output data to report }
Or simplify it a bit
VendAccount partnerCode; //init queryRun Query query = new query(); QueryBuildDatasource qbds; QueryRun queryRun; partnerCode = "V-001, V002"; ; qbds = query.addDataSource(tablenum(VendPackingSlipJour)); qbds.firstOnly(false); qbds.firstFast(false); queryRun = new QueryRun(query); //Build query qbds = queryrun.query().dataSourceTable(tablenum(VendPackingSlipJour)); qbds.addSortField(fieldnum(VendPackingSlipJour, PurchId)); qbds.addSortField(fieldnum(VendPackingSlipJour, createdDateTime)); if (fromDate != dateNull() || toDate != dateNull()) { qbds.addRange(fieldNum(VendPackingSlipJour, createdDateTime)).value(SysQuery::range(fromDate, toDate)); } qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(partnerCode); //Build a report while(queryRun.next()) { queryRun.get(tableNum(VendPackingSlipJour); //output data to report }
Yes I checked both vendors The Vindors are in the same company.
Whole code is
protected void fillDocument() { int n, i; int l = 0; str s = 'Report'; int row = 7; List my_list; container conResult; ListEnumerator my_listEnumerator; ; //build title if (fromDate != dateNull() || toDate != dateNull()) { s = 'с ' date2Str(fromDate, 123, DateDay::Digits2, DateSeparator::Dot, DateMonth::Digits2, DateSeparator::Dot, DateYear::Digits4); s = ' по ' date2Str(toDate, 123, DateDay::Digits2, DateSeparator::Dot, DateMonth::Digits2, DateSeparator::Dot, DateYear::Digits4); } excelWorksheet.range('A2').value2(s); excelWorksheet.bufferStart(); if (partnerCode != '') { my_List = strSplit(partnerCode, ','); my_listEnumerator = my_list.getEnumerator(); //my_listEnumerator.reset(); while (my_listEnumerator.moveNext()) { this.initQuery(); this.progressInit("Unloading data to excel", SysQuery::countTotal(queryrun), #AviTransfer); //BuildQuery (QueryBuildDataSource qbds) this.BuildQuery(my_listEnumerator.current()); //build body of report conResult = this.BuildReportBody(row, l); row = conPeek(conResult, 1); l = conPeek(conResult, 2); } } //build the end of report excelWorksheet.bufferPost(ComExcelDocument_RU::numToNameCell(1,row)); row = l; } protected void BuildQuery (str partCode) { QueryBuildDatasource qbds; qbds = queryrun.query().dataSourceTable(tablenum(VendPackingSlipJour)); qbds.addSortField(fieldnum(VendPackingSlipJour, PurchId)); qbds.addSortField(fieldnum(VendPackingSlipJour, createdDateTime)); if (fromDate != dateNull() || toDate != dateNull()) { qbds.addRange(fieldNum(VendPackingSlipJour, createdDateTime)).value(SysQuery::range(fromDate, toDate)); } if (partCode != '') { qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(SysQuery::value(partCode)); } if (dimPro != '') qbds.addRange(fieldId2Ext(fieldNum(VendPackingSlipJour, Dimension), SysDimension::ALA_Loans 1)).value(SysQuery::value(dimPro)); // if (rca != '') // qbds.addRange(fieldNum(VendPackingSlipJour, InvoiceAccount)).value(SysQuery::value(rca)); } protected container BuildReportBody(int row, int l) { VendPackingSlipJour repTable; PurchTable pTable; DocuRef dr; VendInvoiceJour viJour; container conResult; ; while(queryRun.next()) { repTable = queryRun.get(tableNum(VendPackingSlipJour)); if (VendPackingSlipJour::findRecId(repTable.RecId)) { pTable = PurchTable::find(repTable.PurchId); dr = DocuRef::findRecId(repTable.ALA_DocuRef); viJour = this.findInvoice(repTable.PurchId, repTable.PackingSlipId, dr.RecId); excelWorksheet.bufferAddLine( [pTable.Dimension[5], pTable.RContractAccount, pTable.PurchId, strfmt('%1',pTable.CIT_State), pTable.CurrencyCode, pTable.PurchName, pTable.OrderAccount, dr.Name, repTable.getAmount() repTable.getVAT(), strfmt('%1',repTable.DeliveryDate), strfmt('%1',repTable.createdDateTime), UserInfoHelp::userName(repTable.createdBy), viJour.InvoiceAmount, strfmt('%1',viJour.createdDateTime), UserInfoHelp::userName(viJour.createdBy)]); l ; this.parmProgress().incCount(); if (l >= 2000) { excelWorksheet.bufferPost(ComExcelDocument_RU::numToNameCell(1,row)); excelWorksheet.bufferStart(); row = l; l = 0; } } } conResult = conIns(conResult, 1, row); conResult = conIns(conResult, 2, l); return(conResult); }
After the first iteration the while(queryRun.next()) returns false.
Hi Anton,
Have you checked if both vendors\packing slips are in the same company? Also, you could please, share exact code sample you are using?
André Arnaud de Cal...
291,979
Super User 2025 Season 1
Martin Dráb
230,848
Most Valuable Professional
nmaenpaa
101,156