Get the ranges and values from Query ( Dynamic/AOT ) using X++ in Ax 2012

My Previous Post:Get the ranges and values from Query ( Dynamic/AOT ) using X++ in Ax 2009 

https://jkmsdax2012.wordpress.com/2015/05/05/get-the-ranges-and-values-from-query-dynamicaot-using-x-in-ax-2009/

static void Johnkrish_GetRangeValueOfQuery(Args _args)
{
Query query;
QueryRun queryRun;
QueryBuildDataSource qbd;
GeneralJournalAccountEntry vendTable;
QueryBuildRange range;
QueryFilter qf;
GeneralJournalAccountEntry generalJournalAccountEntry;
int cnt, i;
;

query = new query();
qbd = query.addDataSource(tablenum(GeneralJournalAccountEntry));
queryRun = new QueryRun(query);
queryRun.prompt();
query = queryRun.query();
cnt=query.queryFilterCount();

for (i = 1; i <=cnt ; i++)
{
qf = query.queryFilter(i);
info(strFmt(“Range Field – %1: Value – %2”, qf.field(), qf.value()));
}

while (queryRun.next())
{
generalJournalAccountEntry = queryRun.get(tablenum(GeneralJournalAccountEntry));
info(strfmt(“PostingType – %1, LederAccount – %2”,generalJournalAccountEntry.PostingType, generalJournalAccountEntry.LedgerAccount));
}
}

Output:

QueryWindow

Output

 

Advertisements

Set query range for Two Date ( FromDate To Date) field in a table in Ax 2012

 

static void DateRangeTest(Args _args)
{
DateRangeTest drt;

Query query=new Query();
QueryRun qrun;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
date d=today();
qbds=query.addDataSource(tableNum(DateRangeTest));

qbr=qbds.addRange(fieldNum(DateRangeTest,dataAreaId));
//Method 1
//qbr.value(strfmt(‘(%1<=%2)&& (%3>=%4)’,fieldstr(DateRangeTest,FromDate),Date2StrXpp(d),fieldstr(DateRangeTest,ToDate),Date2StrXpp(d)));
//Method 2
qbr.value(strfmt(‘(FromDate<=%1)&& (ToDate>=%2)’,Date2StrXpp(d),Date2StrXpp(d)));

qrun=new QueryRun(query);
info(qrun.query().dataSourceNo(1).toString());
while(qrun.next())
{
drt=qrun.get(tableNum(DateRangeTest));
info(drt.Name);
}
info(drt.getSQLStatement());
}