web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

Query Extended Range failure.

(0) ShareShare
ReportReport
Posted on by 75

Hello, I'm having an issue with the extended query range syntax. I'm currently trying to generate a query that shows retail discounts based on a starting and ending date range.

The range looks like this.

qbdsStandard = SysQuery::findOrCreateDataSource(qStandard, tableNum(RetailPeriodicDiscount));
SysQuery::findOrCreateRange(qbdsStandard, fieldNum(RetailPeriodicDiscount, ValidFrom))
   .value(strFmt(
      @'(
         (!((RetailPeriodicDiscount.%1 < %3) && (RetailPeriodicDiscount.%2 > %4)))
      && (!((RetailPeriodicDiscount.%1 > %3) && (RetailPeriodicDiscount.%2 < %4)))
      && (!((RetailPeriodicDiscount.%1 > %3) && (RetailPeriodicDiscount.%1 > %4)))
      && (!((RetailPeriodicDiscount.%2 < %3) && (RetailPeriodicDiscount.%2 < %4)))
      )',
   fieldStr(RetailPeriodicDiscount, ValidFrom),
   fieldStr(RetailPeriodicDiscount, ValidTo),
   queryValue(startDate),
   queryValue(endDate)
));

They query will run fine, however an infolog message appears saying "Query extended range failure: The '!' operator can only be used with the 'like' keyword." The range is also ignored. However, if I call toString() on the query and conver the resulting X++ sql into "real" SQL the query and range work fine.

X++ SQL (except * is actually a list of all the fields)

SELECT *
FROM RetailPeriodicDiscount(RetailPeriodicDiscount_1)
WHERE ((DateValidationType = 1)) AND (((
(!((RetailPeriodicDiscount.ValidFrom < "3/21/2015") && (RetailPeriodicDiscount.ValidTo > "3/24/2015")))
&& (!((RetailPeriodicDiscount.ValidFrom > "3/21/2015") && (RetailPeriodicDiscount.ValidTo < "3/24/2015")))
&& (!((RetailPeriodicDiscount.ValidFrom > "3/21/2015") && (RetailPeriodicDiscount.ValidFrom > "3/24/2015")))
&& (!((RetailPeriodicDiscount.ValidTo < "3/21/2015") && (RetailPeriodicDiscount.ValidTo < "3/24/2015")))
)))
JOIN * FROM RetailPeriodicDiscountLine(RetailPeriodicDiscountLine_1)
ON RetailPeriodicDiscount.OfferId = RetailPeriodicDiscountLine.OfferId
JOIN * FROM RetailGroupMemberLine(RetailGroupMemberLine_1) ON RetailPeriodicDiscountLine.RetailGroupMemberLine = RetailGroupMemberLine.RecIdJOIN * FROM EcoResProduct(EcoResProduct_1) ON RetailGroupMemberLine.Product = EcoResProduct.RecId

I then convert it (by hand) into what I think would be the equivalent sql.

REAL SQL

SELECT *
FROM RetailPeriodicDiscount d

JOIN RetailPeriodicDiscountLine dl
ON d.OfferId = dl.OfferId
JOIN RetailGroupMemberLine gml
ON dl.RetailGroupMemberLine = gml.RecId
JOIN EcoResProduct p
ON gml.Product = p.RecId

WHERE ((d.DateValidationType = 1))
AND (((
(not((d.ValidFrom < '3/21/2015') and (d.ValidTo > '3/24/2015')))
and (not((d.ValidFrom > '3/21/2015') and (d.ValidTo < '3/24/2015')))
and (not((d.ValidFrom > '3/21/2015') and (d.ValidFrom > '3/24/2015')))
and (not((d.ValidTo < '3/21/2015') and (d.ValidTo < '3/24/2015')))
)))

Basically, the real sql executes correctly, and the correct rows are returned with the date ranges. However, the X++ sql just returns everything with absolutely no filtering. Is there a way to do this in X++? Am I overlooking something?


*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    The extended query syntax is more a hack than a feature and you have to be vary careful regarding the syntax that AX internally uses when building query strings, otherwise you get into troubles. You won't get any compile-time control nor any reasonably diagnostics.

    I immediately see two errors:

    1. The operator you want to use is NOT (instead of !)
    2. Don't use the hard-coded data source name (RetailPeriodicDiscount) - it may be different (e.g. RetailPeriodicDiscount_1). Take the value from qbdsStandard.name().
  • RyanV Profile Picture
    75 on at

    Thanks for the reply Martin.

    Unfortunately using the "not" keyword I'm getting "Query extended range failure: Syntax error near 0." It does not appear to like negations in boolean expressions. I'm going to re-write the expression without using !/not to achieve the same results, which is a little bit of a pain, but oh well.

    I also changed my code to use the .name() call for retrieving the datasource name.

  • Verified answer
    Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    I did few tests and it seems that you really can't invert whole expressions (or at least I failed). Even if I copy a string generated by AX from a normal range and paste it to an extended range value, it fails.

    You'll have to negate operators (= → !=, < → >= etc.).

  • RyanV Profile Picture
    75 on at

    Yep. It's kind of a pain because I have to dig out a pen and paper and make sure that all tests pass in my head at least. I guess this would be the perfect use case for unit testing in AX ;)

  • Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    You can do it - AX comes with a unit test framework out of the box.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Sanhthosh.Kumar.K Profile Picture

Sanhthosh.Kumar.K 2

#2
Raed Salah Bzour Profile Picture

Raed Salah Bzour 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans