How to get query filters?

Hi All,

I would like to get query filters but this code doesn’t get correct filters. Where I’m wrong?

for (i=1;i<=qrun.query().dataSourceCount();i++)
{
for (j=1;j<=qrun.query().dataSourceNo(i).rangeCount();j++)
{
info(strfmt("%1 %2",qrun.query().dataSourceNo(i).range(j).name(),qrun.query().dataSourceNo(i).range(j).value()));
}
}

And I see some messages at this from, code parts are seem different. How can I do this? What tag I should use?

What do you mean by query filters in AX 2009? And what does the code snippet to do with your question?
Please realize that we can’t help you if you don’t explain your problem.

Note that I have no idea what you mean by “I see some messages at this from, code parts are seem different”.

Sorry, my poor english…

User adds some filters to query and I have to see if user adds filters to query. like this:

CustAccount - CS00001…CS00050

TransDate - 15.11.2015

Of course I don’t use filters with infolog, this was just test for if I could got filters correct.

You answered my question at this thread: http://dynamicsuser.net/forums/p/87099/472367.aspx#472367

Your code snip seem different and I just wondered what I should do for show my code parts like yours.

I looked up source of page, saw code tags, tried without success..

I’ll assume that you mean QueryBuildRange objects when talking about filters. For your information, note that AX 2012 supports query filters, therefore the distinction between ranges and filters (and potentially other concepts) is important.

Nevertheless I still don’t understand your problem. I also can’t comment on your piece of code without context.

This’s an AX 2009 case. I just wanted to know if user add filter/range to query. They want to see if there’re user defined filters in query before press select button. I’ll put an information text “There’re filters on query” / “There isn’t any filters on query”.

And what’s the problem?

Okey…

User selects CustAccount for query filter and wrote something like MK0002*

But code at I wrote first message I get wrong informations. Wrong field name and wrong range value. That’s the problem.

User selects cust account for the filter and writes MU0005* for range. I get wrong field name and wrong range value with code I wrote at the first message of this thread.

Let me add the context to your code, so we get something executable:

int i, j;
Query  = new Query();
QueryBuildDataSource ds = query.addDataSource(tableNum(SalesTable));
QueryBuildRange range;

ds.addRange(fieldNum(SalesTable, CustAccount).value('MU0005*');

for (i=1; i<=query.dataSourceCount(); i++)
{
    for (j=1; j<=query.dataSourceNo(i).rangeCount(); j++)
    {
	range = query.dataSourceNo(i).range(j);
        info(strfmt("%1 %2", range.name(), range.value()));
    }
}

I don’t have access to AX today, therefore could you please execute it and tell us what result you get?

It gives correct value. I don’t know what’s the problem now. In my case just users select ranges with select button of query, not with X++ code. :frowning:

Anyway thank you…

It gives correct result. I don’t understand. In my case just users select query ranges with visual select button, not with X++ code… :frowning:

Anyway, thank you…

It means that your problem somewhere else, but we can’t tell you where, because you didn’t give us much information except the piece of code (which happened not to be the cause).

For example, you may be working with a wrong QueryRun object.

I just wanted don’t bother people at there so much, because all people are there helps each other with free. my real code is:

This’s my query create code:

void createAndSaveQueryInTable()
{
SysQueryRun qRun;
Query q;
QueryBuildDataSource qbds;
QueryBuildRange qRange;
;
if (this.InventTableQuery)
qRun = new SysQueryRun(this.InventTableQuery);
else
{
q = new query();
qbds = q.addDataSource(tablenum(InventTable));
qbds = qbds.addDataSource(tablenum(RBOInventTable));
qbds.relations(true);
qbds = qbds.addDataSource(tablenum(PriceDiscTable));
qbds.addLink(fieldnum(RBOInventTable,ItemId),fieldNum(PRiceDiscTable,ItemRelation));
qRange = qbds.addRange(fieldNum(PriceDiscTable,ItemCode));
qRange.value(QueryValue(TableGroupAll::Table));
qRange.status(RangeStatus::Hidden);
qRun = new SysQueryRun(q);
}
qrun.promptLoadLastUsedQuery(false);
if (qRun.prompt())
this.InventTableQuery = qRun.pack();
}

I lookup if query created before with this code, if created, I use query. If not, I create query and show. After that I save the query to table.

I put this code to my tables active method. I supposed if query has range/filter my checkbox will be checked, not unchecked:

boolean checkIfQueryFilter(SysQueryRun qRun)
{
Query q;
int i,j,k;
;
for (i=1;i<=qrun.query().dataSourceCount();i++)
{
for (j=1;j<=qrun.query().dataSourceNo(i).rangeCount();j++)
{
// info(strfmt("%1 %2",qrun.query().dataSourceNo(i).range(j).name(),qrun.query().dataSourceNo(i).range(j).value()));
if (qrun.query().dataSourceNo(i).range(j).value())
return true;
}
}
return false;
}

In my case there isn’t any filter but when I unchecked info’s comment it shows:

Dimension[3] !01

There isn’t any filter with dimension…

Don’t just observe the result - debug the problem to see where exactly it goes wrong.

First of all, verify that your query really contains the ranges. There may be many reasons why it’s not, such as because you don’t actually save the record or because you don’t correctly pack and unpack object state (none of these things is shown in your code).

One of several ways to see if the ranges are there is calling qrun.query().xml().

Next steps depend on your findings.

Actually my pack and unpack code is up. See:

this’s unpack:

if (this.InventTableQuery)
qRun = new SysQueryRun(this.InventTableQuery);

this’s pack:

if (qRun.prompt())
this.InventTableQuery = qRun.pack();

Also this’s my unpack when I check query if empty or not:

qRun = new SysQueryRun(DMRReqDemPromotionImpact.InventTableQuery);
fRetail.value(element.checkIfQueryFilter(qRun));

I’ll try xml method too. Thank you…

For some reason, I thought you have a RunBase or SysOperation class, where you would need some extra logic for packing. Ignore my remark if it’s not the case, it was just an example anyway.

Let’s discuss the context in which you use your method only if needed, after you verify which part is wrong.

I don’t understand.

info(qrun.query().dataSourceNo(1).toString());
if (qRun.prompt())
this.InventTableQuery = qRun.pack();
info(qrun.query().dataSourceNo(1).toString());

There is no dimension filter in first info and there’s a dimension filter at second info. How can that be possible???

This’s the first info’s result:

SELECT FIRSTFAST * FROM InventTable JOIN FIRSTFAST * FROM RBOInventTable WHERE InventTable.ItemId = RBOInventTable.itemId JOIN FIRSTFAST * FROM PriceDiscTable WHERE RBOInventTable.itemId = PriceDiscTable.ItemRelation AND ((ItemCode = 0))

That’s the second info’s result:

SELECT FIRSTFAST * FROM InventTable WHERE ((NOT (Dimension[3] = N’01’))) JOIN FIRSTFAST * FROM RBOInventTable WHERE InventTable.ItemId = RBOInventTable.itemId JOIN FIRSTFAST * FROM PriceDiscTable WHERE RBOInventTable.itemId = PriceDiscTable.ItemRelation AND ((ItemCode = 0))

I think you get a previously saved query.

Yes, I get a previously saved query but why in first info, just before prompt doesn’t have dimension filter?

info(qrun.query().dataSourceNo(1).toString());
if (qRun.prompt())

If I don’t run prompt my saved query never show dimension filter.

Because it’s before loading the saved query in prompt().