Skip to main content



No record found.

Microsoft Dynamics AX forum
Suggested answer

clearRanges before filter by field

Posted on by 925

I have a form with 1 queryBuildRange on it and executes the range in the ExecuteQuery() method on the datasource. How can I clearranges before filtering for specific value (when doing right-click mouse on formgrid and filterByField)?


  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: clearRanges before filter by field

    You can use the method SysQuery::findOrCreateRange(...). It will check if the range already exists and reuse it, otherwise it creates it.

  • Glenn Lissone Profile Picture
    Glenn Lissone 925 on at
    RE: clearRanges before filter by field

    If I follow your first method is it then also possible to set the value, when user filter with right-click mouse "filterbyfield" back to the combobox? If not is it then possible to disable right-mouse click filterbyfield?

  • Suggested answer
    Amir Nazim Profile Picture
    Amir Nazim 5,994 on at
    RE: clearRanges before filter by field

    executeQuery method is not the right place to code for this scenario? as it always executes.

    instead  try moving your code in Init method of datasource.

  • Suggested answer
    DG Profile Picture
    DG 1,226 on at
    RE: clearRanges before filter by field

    Hi Glenn,

    The 'filter by field' feature is designed to put a filter on the existing query. What you are trying to do is reset the existing query and then apply filter. So, in my opinion, this is not a good design. If I were you, I would have put extra drop down filter(s) on the top of form and populate them with relevant values; for example 'user A' and project status 'finished'. And apply filter in selection changed methods of these drop-down boxes. So user would have visibility and flexibility to set filters and view relevant data.

    Now that being said, If you still want to go ahead with your idea

    1. Move the code of initializing the filter ranges to the 'init' method of the datasource.

    2. Override the 'context' method of the field in question. You need to write your own 'Filter by field' code here. You have the option to completely surpress the standard context menu and just have your menu options. Alternatively you can append your option at the end of standard menu list. For more how to do that see this blog

    3. Please ensure that you also put code for removing the filter. That will be for setting initial ranges back.

    Hope this helps.



  • Glenn Lissone Profile Picture
    Glenn Lissone 925 on at
    RE: clearRanges before filter by field

    which statement do I need as if and else criteria? So when the forms open .addrange and when user do right-click mouse remove alle ranges and filter by field?

    public void executeQuery()



       //what is the criteria needed in the if/else statement?

       //if filter not used and form is default opened

           this.query().dataSourceNo(1).addRange(fieldNum(projTable, responsible)).value('userA');

           this.query().dataSourceNo(1).addRange(fieldNum(projTable, status)).value(enum2str(projStatus::finished));

       //else when filter is set via "right-mouse click-filter by field"

           //1. first remove all ranges  


           //2. apply new filters by field



  • Jo Neetesonne Profile Picture
    Jo Neetesonne 175 on at
    RE: clearRanges before filter by field

    Not sure what you want to achieve, but clearing a specific range can be done like this:

    queryBuildDataSource.clearRange(fieldNum('Table name','Field name'));

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .


André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,989 Super User

Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

nmaenpaa Profile Picture

nmaenpaa 101,148


Featured topics

Product updates

Dynamics 365 release plans