Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Not expired Query Range for bank accounts

Posted on by 77

I'm trying to get a QueryRangeFunction to return the valid string for a vendbankAccount that has not expired yet, which means

ExpiryDate = dateMin() OR ExpiryDate > today()

The existing QueryRange functions don't seem to be able to do that, but are valid examples for trying to write my own function:

[QueryRangeFunction]
public static str BankAccountNotExpiredYet()
{
return strFmt("(( %1.%2 = \"%3\") || (%1.%2 > \"%4\"))",
TableStr(VendBankAccount),
fieldStr(VendBankAccount, ExpiryDate),
DateTimeUtil::minValue(),
DateTimeUtil::utcNow());
}

This method returns 

(( VendBankAccount.ExpiryDate = "") || (VendBankAccount.ExpiryDate > "8/8/2019 01:15:48 pm"))

However this doesn't filter the bank accounts that aren't expired at all.

Have spent a few hours on something that should really be trivial, any help would be appreciated,

Thanks

*This post is locked for comments

  • Suggested answer
    MagVesse Profile Picture
    MagVesse 77 on at
    RE: Not expired Query Range for bank accounts

    Beautiful mate,

    I used option #1 in the tiles query, works great

    I used option #2 in form datasources, works great

    I used option #3 for computing charts results using while selects, works great, although I knew that one!

    Thanks for your help, I had given up on #1 and #2... I think my main mistake was to expect a combined solution for #1 and #2 for both tiles and datasources... Works like a charm, eternally grateful!

  • Verified answer
    Khushhal Garg Profile Picture
    Khushhal Garg 1,514 on at
    RE: Not expired Query Range for bank accounts

    Hi Greg,

    Here are 3 different options. Either of these can be used to achieve your results.

    Option1:

    Here is the correct string to be built in QueryRangeFunction

    return  strFmt('(%1 = %2) || (%1 > %3)',

           fieldStr(VendBankAccount, ExpiryDate),

               queryValue(DateTimeUtil::minValue()),

               queryValue(DateTimeUtil::utcNow()));

    Option2:

    Following query also gives correct results.

           Query qr;

           QueryBuildDataSource qbds;

           qr = new Query();

           qbds = qr.addDataSource(tableNum(VendBankAccount));

           qbds.addRange(fieldNum(VendBankAccount, ExpiryDate)).value(queryValue(DateTimeUtil::minValue()));            

           qbds.addRange(fieldNum(VendBankAccount, ExpiryDate)).value(strfmt('>%1', DateTimeUtil::utcNow()));

    Option3:

    Create macro (%1.(%2) == %3) || (%1.(%2) > %4) and use it like this. It is also giving correct count of records.

    select count(RecId) from vendBankAccount where #expiryDateQuery(vendBankAccount, fieldnum(VendBankAccount, ExpiryDate), DateTimeUtil::minValue(), DateTimeUtil::utcNow());

  • MagVesse Profile Picture
    MagVesse 77 on at
    RE: Not expired Query Range for bank accounts

    Ideally, I want to use this feature in both a form grid and a tile query, so that's why I made it a function with attribute QueryRangeFunction.

  • MagVesse Profile Picture
    MagVesse 77 on at
    RE: Not expired Query Range for bank accounts

    Hi Khushhal,

    See this screenshot: https://imgur.com/paUHqrX

    Nothing too fancy, I reproduced this issue very simply:

    Added the method to Class1,

    Created a new Form1, added VendBankAccount as datasource, added the filter in the init method, and dropped a few fields in the grid,

    Date is 8/18 in US format, when the form opens, it shows all records in VendBankAccount, with mindate, with date<today and date>today.

    Thanks

  • Suggested answer
    Khushhal Garg Profile Picture
    Khushhal Garg 1,514 on at
    RE: Not expired Query Range for bank accounts

    How are you using function BankAccountNotExpiredYet()? Can you provide some more info or screenshot where you are using this function? Since this function returns str type so can't be used directly with where clause so want to see how you are using it?

    Also, modify following like below but it won't resolve your issue but this is the correct syntax to use these date values here.

    SysQuery::value(DateTimeUtil::minValue()),

    SysQuery::value(DateTimeUtil::utcNow());

  • MagVesse Profile Picture
    MagVesse 77 on at
    RE: Not expired Query Range for bank accounts

    Hi Khushhal,

    I tried the following, but it generates the following error, hence I had added extra parenthesis and quotes.

    Query extended range failure: Syntax error near 34.

    QueryString for the query is:

    SELECT FIRSTFAST * FROM VendBankAccount(VendBankAccount) WHERE ((( VendBankAccount.ExpiryDate = ) || (VendBankAccount.ExpiryDate > 8/18/2019 10:38:13 am)))

    Regards,

  • Suggested answer
    Khushhal Garg Profile Picture
    Khushhal Garg 1,514 on at
    RE: Not expired Query Range for bank accounts

    Can you try this?

    return strFmt('( %1.%2 = %3) || (%1.%2 > %4)',

            TableStr(VendBankAccount),

            fieldStr(VendBankAccount, ExpiryDate),

            DateTimeUtil::minValue(),

            DateTimeUtil::utcNow());

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

Featured topics

Product updates

Dynamics 365 release plans