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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Query range in execute query D365 F&O

(1) ShareShare
ReportReport
Posted on by 265
Hi All,
 
I am getting "invalid range" error when clicking the button. Below is the relevant code, I want to filter the form based on a toggle control.
If the toggle is checked, the filter condition should be:
InvoiceDate <= Today() - TotalPeriod
However, TotalPeriod represents a number of months (e.g., 2 or 3). the intended logic should be
InvoiceDate <= AddMonths(Today(), -TotalPeriod). Any suggestions/help would be greatly appreciated.
 

 

if (Cntrl. checked())

{

 qbds.addRange(fieldnum(CustInvoiceTable, TotalPeriod));
 

QueryBuildRange criteriaCreditl = qbds.addRange(fieldnum(CustInvoiceTable, InvoiceDate)):

 

criteriaCreditl.value(strFmt('(%1 < =(date2StrXpp(DateTimeUtil::date(DateTimeUtil::addmonths(today(), -%2))))', fieldnum(CustInvoiceTable, InvoiceDate), fieldnum(CustInvoiceTable, TotalPeriod)));
}
next executeQuery();

 

 

 


Thanks!


 

 

 

 

Categories:
I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    301,293 Super User 2025 Season 2 on at
    Hi,
     
    In case you need a range on the invoice date before your calculated date, your range value has a wrong syntax.
     
    The result should be something like: '.. 23/05/2025'

    Your current range value has a complete wrong syntax where the outcome should be something like this: '(32 < =(date2StrXpp(DateTimeUtil::date(DateTimeUtil::addmonths(today(), -47))))'. You can check this by using the debugger.

    Change your coding to:
     
    criteriaCreditl.value(strFmt('.. %1', date2StrXpp(DateTimeUtil::date(DateTimeUtil::addmonths(today(), CustInvoiceTable.TotalPeriod)))));
     
  • Martin Dráb Profile Picture
    238,024 Most Valuable Professional on at
    You can also use queryRange() to avoid the format string completely. In my example, I've fixed one more issues (that today() shouldn't be used). I'm assuming that criteriaCreditl refers to a date field; it it was an utcDateTime field, you would need to use UTC time instead.
    utcDateTime today = DateTimeUtil::getToday(DateTimeUtil::getUserPreferredTimeZone());
    utcDateTime adjustedDate = DateTimeUtil::date(DateTimeUtil::addMonths(today, custInvoiceTable.TotalPeriod));
    criteriaCreditl.value(queryRange(dateNull(), adjustedDate));
     
  • Suggested answer
    Martin Dráb Profile Picture
    238,024 Most Valuable Professional on at
    It seems that your intention is your intention is to calculate an adjusted date based on two fields of the same record and then use it in a comparison with a different field. You have two options:
    1. Either you do it in database, but then you can't use X++ code such as DateTimeUtil::addmonths().
    2. Or you'll first fetch the record to F&O, calculate the value by X++ and use the result in the filter. 
    Your current attempt to create a SQL condition containing X++ functions can't work.
     
    Consider creating a view with a computed column that will calculate the date based on InvoiceDate and TotalPeriod. Instead of X++ function DateTimeUtil::addMonths(), you'll need T-SQL function DATEADD(). You can then simply compare the other field with the computed column.

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

News and Announcements

Season of Giving Solutions is Here!

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
Abhilash Warrier Profile Picture

Abhilash Warrier 677 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 271 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans