Store Query Range in variables


I have the following query and I want to store in two variables fields ‘dtFromDate’ and ‘dtToDate’ in order to use them in other methods. As it is now the code, they return null when I use them. How should I do this?

TransDate dtFromDate,dtToDate;

qbd2 = qbd1.addDataSource(tablenum(assettrans));
qbd2.addRange(fieldnum(AssetTrans, TransDate)).value(SysQuery::range(dtFromDate, dtToDate));

qbd2.addSortField(FieldNum(assettrans, TransDate));



Try this code

QueryRun qr;

QueryBuildDataSource qbds;

QueryBuildRange qbr,qbr1;


qr = new QueryRun(element);

qbds = element.query().dataSourceTable(tablenum(VendTrans));

qbds.addSortField(fieldnum(VendTrans, Transdate), SortOrder::Ascending);

qbr = qbds.addRange(fieldnum(VendTrans, Transdate));

qbr.value(queryrange(FromDate, ToDate));

qbr2 = qbds.addRange(fieldnum(VendTrans, Dimension));


Where do you store the variables and how do you use them?


I want to store ranges in global variables in order to use them. For example in the following job the infolog is empty for 'dtFromDate '.

Is there any way to retreive the value from dtFromDate ?

static void Job119(Args _args)


Query q;

QueryRun qr;

QueryBuildDataSource qbd,qbd1,qbd2,qbd3,qbd4;

QueryBuildRange qbr,qbr1;

assettrans tassettrans;

TransDate dtFromDate;

TransDate dtToDate;

q = new Query();

qbd = q.addDataSource(TableNum(assettrans));

qbd.addRange(fieldnum(AssetTrans, TransDate)).value(SysQuery::range(dtFromDate, dtToDate));

qbd.addSortField(FieldNum(assettrans, TransDate));

qr = new QueryRun(q);

If( qr.prompt() )


while ( )


tassettrans = qr.get(tableNum(assettrans));






Your current code doesn’t ever set any value to those variables, so they will be always empty.

You can get values either from parameters, or from internal state of an object. In the latter case, you need to move your code to a class and turn the variables to instance ones (i.e. declared in ClassDeclaration). Then you have to set the values somewhere. If you want to use parameters, move the code to a table method or a class method, turn the variables to parameters and call the method with required values.

whats your actual object in AX, its a Job or report?

and from where you are passing these date values?


I made a class for this and I used class SysQuery::FindorCreateRange to retrieve date parameters.

Then I called the class from a button in a form.

Thank you all for your help.