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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Date filter on a Datetime field in a AX View is not functioning.

(0) ShareShare
ReportReport
Posted on by 86

Query build on runtime taken from debugger is this

{SELECT * FROM TestQAInspectionLogView(TestQAInspectionLogView) ORDER BY TestQAInspectionLogView.QualityOrderId ASC, TestQAInspectionLogView.InventRefId ASC, TestQAInspectionLogView.InventRefTransId ASC, TestQAInspectionLogView.ItemId ASC, TestQAInspectionLogView.TestGroupId ASC, TestQAInspectionLogView.TestId ASC, TestQAInspectionLogView.DocuRefRecId ASC, TestQAInspectionLogView.DocuRecid ASC WHERE (((QualityOrdDate>= 9/1/2019)) OR ((QualityOrdDate<= 9/30/2019)))}

Any Ideas that the field QualityOrdDate is a Datetime field in the View. 

Thanks

Raj

I have the same question (0)
  • Dilliraj Pudi Dasarathan Profile Picture
    86 on at

    Although i tried to modify the queries in different format  like this

    {SELECT * FROM TestQAInspectionLogView(TestQAInspectionLogView) ORDER BY TestQAInspectionLogView.QualityOrderId ASC, TestQAInspectionLogView.InventRefId ASC, TestQAInspectionLogView.InventRefTransId ASC, TestQAInspectionLogView.ItemId ASC, TestQAInspectionLogView.TestGroupId ASC, TestQAInspectionLogView.TestId ASC, TestQAInspectionLogView.DocuRefRecId ASC, TestQAInspectionLogView.DocuRecid ASC WHERE (((QualityOrdDate<= "9/1/2019".."9/30/2019")))}

    Above query throws this Error

    Query extended range failure: Right parenthesis expected near pos 31.

    {SELECT * FROM TestQAInspectionLogView(TestQAInspectionLogView) ORDER BY TestQAInspectionLogView.QualityOrderId ASC, TestQAInspectionLogView.InventRefId ASC, TestQAInspectionLogView.InventRefTransId ASC, TestQAInspectionLogView.ItemId ASC, TestQAInspectionLogView.TestGroupId ASC, TestQAInspectionLogView.TestId ASC, TestQAInspectionLogView.DocuRefRecId ASC, TestQAInspectionLogView.DocuRecid ASC WHERE (((QualityOrdDate<= 2019-09-01T00:00:00..2019-09-30T00:00:00)))}

    Query extended range failure: Right parenthesis expected near pos 40.

    {SELECT * FROM TestQAInspectionLogView(TestQAInspectionLogView) ORDER BY TestQAInspectionLogView.QualityOrderId ASC, TestQAInspectionLogView.InventRefId ASC, TestQAInspectionLogView.InventRefTransId ASC, TestQAInspectionLogView.ItemId ASC, TestQAInspectionLogView.TestGroupId ASC, TestQAInspectionLogView.TestId ASC, TestQAInspectionLogView.DocuRefRecId ASC, TestQAInspectionLogView.DocuRecid ASC WHERE (((QualityOrdDate>= 2019-09-01T00:00:00)) OR ((QualityOrdDate<= 2019-09-30T00:00:00)))}

    No Error and Filter doesn't apply

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    What problem do you have with the query?

    And what do you mean by "Any Ideas that the field QualityOrdDate is a Datetime field"?

  • Dilliraj Pudi Dasarathan Profile Picture
    86 on at

    Hi Martin,

    Yes the QualityOrdDate is a Date Time Field ,   Converting the Date field from dialog to date time by using DateTimeUtil::toStr(FromDate)  and passing it to the query range value doesn't work despite the range shows the values with the Date Time.

    In the First Instance, i tried this SysQuery::range (FromDate, ToDate)  which didnot work

    I had to Literally use SysQuery::range (fDateTime, toDateTime ) But Before that i need to convert the values of FromDate to FdateTime   like this.

               fDateTime = DateTimeUtil::newDateTime(fromDate,str2time("00:00:00 AM"),DateTimeUtil::getUserPreferredTimeZone());

    Otherwise it doesn't work as it is my fault and systems fault as the system didn't throw compilation build error if i use the code like this

    qbrTDate.value( xSysQuery::range(DateTimeUtil::toStr(fromDate),DateTimeUtil::toStr(toDate)));

    I was passing a Date field to DateTimeutil::tostr command , system didn't throw any error in build but the filter was not working

    Thanks For your time Martin for Replying on the Post.

    Any how solution is

    We can close this thread as below  solution works fine.

    1. I had to convert the date fields to datetime with timezone preference and then

    2. SysQuery::range(1st date, 2nddate) works fine.

    Thanks

    Raj

  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    Let's forget code for a moment and clarify what you're trying to achieve. It seems that you get two days from a user and you're trying to use them in X code to filter a view. You want to add a filter on a utcDateTime and want to get back only values between those two dates. Is that correct?

    If so, all you need is queryRange(). There is no need to covert the types, and by the way, don't use str2time("00:00:00 AM") for this purpose anyway. You can simply use 0 instead.

    Let me give you a concrete example that you can copy and run (you'll just need to modify the dates to match your data).

    Query q = new Query();
    date fromDate = mkDate(1,8,2019);
    date toDate = mkDate(31,8,2019);
    
    QueryBuildDataSource ds = q.addDataSource(tableNum(SalesOrderLastChangeDateV2));
    QueryBuildRange range = ds.addRange(fieldNum(SalesOrderLastChangeDateV2, HeaderLastModifiedDateTime));
    range.value(queryRange(fromDate, toDate   1));
    
    info(ds.toString());
    
    QueryRun qr = new QueryRun(q);
    while (qr.next())
    {
        SalesOrderLastChangeDateV2 view = qr.getNo(1);
        info(any2Str(view.HeaderLastModifiedDateTime));
    }

    This the pseudo-SQL code returned from ds.toString():

    SELECT * FROM SalesOrderLastChangeDateV2(SalesOrderLastChangeDateV2_1)
    WHERE ((HeaderLastModifiedDateTime>='2019-08-01T00:00:00' AND HeaderLastModifiedDateTime<='2019-09-01T00:00:00'))

    It's what you wanted, isn't it?

  • Jay Barot Profile Picture
    1,502 on at

    Dear Raj,

    can we use below fundtions to convert your date to datetime and run the query ?

    periodstart = DateTimeUtil::newDateTime(mkDate(01,09,2019),0)

    periodend =  DateTimeUtil::newDateTime(mkDate(30,09,2019),86400)

    Thanks,

    Jay

  • Verified answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    You could make such a conversion, although I've just demonstrated that it's not needed.

    Also, please avoid magical numbers such as 86400, because it doesn't reveal your intent. You can use #TimeConstants macro library, for example, with macros like #secondsPerDay and #LastSecondOfTheDay. Or define your own named constant instead of just using an unnamed number.

  • Dilliraj Pudi Dasarathan Profile Picture
    86 on at

    I Will try this way. Thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans