x++


Advanced query range


How to make a query in Ax with advanced filtering (with x++):
I want to make such filter criteria On SalesTable form to show SalesTable.SalesId == "001" || SalesLine.LineAmount == 100.
So result should show SalesOrder 001 AND other salesOrders which has at least one SalesLine with LineAmount = 100?
The AX select statement supports exists join such as:
while select salesTable
exits join salesLine
where salesLine.SalesId == salesTable.SalesId &&
salesLine.LineAmount == 100
X++ does not support exists clause as a subquery in the where clause. Therefore it is not possible to express the exists in combination with or.
However AX supports query expressions in a query.
Therefore your query should be possible to express like this:
static void TestQuery(Args _args)
{
SalesTable st;
QueryRun qr = new QueryRun(new Query());
QueryBuildDataSource qst = qr.query().addDataSource(tableNum(SalesTable));
QueryBuildDataSource qsl = qst.addDataSource(tableNum(SalesLine));
str qstr = strFmt('((%1.SalesId == "%2") || (%3.LineAmount == %4))',
qst.name(), queryValue("001"),
qsl.name(), queryValue(100));
qsl.relations(true); // Link on SalesId
qsl.joinMode(JoinMode::ExistsJoin);
qsl.addRange(fieldNum(SalesLine,RecId)).value(qstr);
info(qstr); // This is the query expression
info(qst.toString()); // This is the full query
while (qr.next())
{
st = qr.get(tableNum(SalesTable));
info(st.SalesId);
}
}
However, if sales order 001 does not contain lines, it will not be selected.
Other than that the output is as you requested:
((SalesTable_1.SalesId == "001") || (SalesLine_1.LineAmount == 100))
SELECT FIRSTFAST * FROM SalesTable
EXISTS JOIN FIRSTFAST * FROM SalesLine WHERE SalesTable.SalesId =
SalesLine.SalesId AND ((((SalesTable_1.SalesId == "001") ||
(SalesLine_1.LineAmount == 100))))
001
125
175
Jan's solution works fine if sales order '001' should only be selected if it has sales lines. If it doesn't have lines it won't appear in the output.
If it is important to you that sales order '001' should always appear in the output even if it doesn't have sales lines, you can do it via union as follows:
static void AdvancedFiltering(Args _args)
{
Query q;
QueryRun qr;
QueryBuildDataSource qbds;
SalesTable salesTable;
;
q = new Query();
q.queryType(QueryType::Union);
qbds = q.addDataSource(tablenum(SalesTable), identifierstr(SalesTable_1));
qbds.fields().dynamic(false);
qbds.fields().clearFieldList();
qbds.fields().addField(fieldnum(SalesTable, SalesId));
qbds.addRange(fieldnum(SalesTable, SalesId)).value(queryValue('001'));
qbds = q.addDataSource(tablenum(SalesTable), identifierstr(SalesTable_2), UnionType::Union);
qbds.fields().dynamic(false);
qbds.fields().clearFieldList();
qbds.fields().addField(fieldnum(SalesTable, SalesId));
qbds = qbds.addDataSource(tablenum(SalesLine));
qbds.relations(true);
qbds.joinMode(JoinMode::ExistsJoin);
qbds.addRange(fieldnum(SalesLine, LineAmount )).value(queryValue(100));
qr = new QueryRun(q);
while (qr.next())
{
salesTable = qr.get(tablenum(SalesTable));
info(salesTable.SalesId);
}
}

Related Links

X++ unbound control
WeekOfYear in X++ using .net lib
How to tell what module has called the program
Getting the index of an array element of EDT Dimension
Destroy Object in X++
not able to run job in batch
Creating a folder on fileSystem dynamics ax
Can the X++ language be used for the following development?
How can you create a simple dialog box in Dynamics AX?
Reading a comma separated values (csv) file in dynamics ax
What is the function to get the system date and current date in Dynamics AX?
Dynamics AX 2009 - Modify insert into smmActivities from ProjTableWizard?
Axapta: How can the Table Name, Not the Table ID, be used as a select in the Select Dialog of the Database Log Report?
How do I get the previous field value in the modifiedField method of a Dynamic Ax table?
What is the best way to print columns from different tables on the same row using the Axapta reporting tool?
Page breaks in Dynamic Ax Reports

Categories

HOME
shell
httparty
fabricjs
webdriver
themes
battery
solver
cublas
vxml
3d-reconstruction
settings
angularfire2
nstableheaderview
modal-dialog
rcloud
okhttp
drop-down-menu
appcelerator-studio
text-mining
mxgraph
cmdb
bourbon
symbols
googlebot
angular-routing
pic32
fragment-backstack
has-and-belongs-to-many
8051
angular-translate
ntp
android-sugarorm
keras-layer
pop3
installscript
salesforce-communities
playframework-2.1
sessionstorage
odroid
preloader
ssrs-2014
berkeley-db-je
show-hide
proget
compiler-design
backbone-collections
cup
janus
google-cloud-console
dotnetnuke-7
refinerycms
php-socket
turbo-prolog
thread-sleep
grass
invoke-sqlcmd
exim4
themoviedb-api
wiredep
ember-cli-rails
iced-coffeescript
skspritenode
mmwormhole
code-documentation
easyrtc
device-tree
tweenlite
reflector
winginx
wxformbuilder
clickbank
ecslidingviewcontroller
rfc5322
android-usb
rolify
ui-select2
font-awesome-4.0.0
superscrollorama
mantle
bjyauthorize
resource-management
apache-whirr
predicatebuilder
box2d-iphone
role
jqote
tournament
w3c-geolocation
tmx
build-environment
alsb
localizable.strings
int64
information-management

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App