Skip to main content

Notifications

Announcements

No record found.

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

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

    I Will try this way. Thanks

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Date filter on a Datetime field in a AX View is not functioning.

    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.

  • Jay Barot Profile Picture
    Jay Barot 1,502 on at
    RE: Date filter on a Datetime field in a AX View is not functioning.

    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

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Date filter on a Datetime field in a AX View is not functioning.

    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?

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

    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

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Date filter on a Datetime field in a AX View is not functioning.

    What problem do you have with the query?

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

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

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans