Cluke
Cluke

Reputation: 11

Adding a form filter

I'm currently working on a form in Microsoft Dynamics AX. The form consists of a grid with about 10 fields from 4 different tables. As the form is now it returns too many values so I need to include some sort of filter, it doesn't need to be dynamic, just a static filter saying only show the lines with value X in column Y.

Has anyone here got some experience with this sort of thing? Where do I start?

I must say I'm not experienced with Microsof AX at all, I've been working with it for about a month now.

I've tried to follow this guide: How to: Add Filter Controls to a Simple List Form [AX 2012] But I got stuck at the second part (To add a control to the custom filter group) Step 2: I dont know which type of control to chose, and ik i pick lets say a ComboBox i cant get Step 3 to work because I dont see the 'Override Methods' they mention.

Upvotes: 0

Views: 4330

Answers (1)

Bull
Bull

Reputation: 758

Well, I usually do it this way:

  • In ClassDeclaration, create as many QueryBuildRanges variables as fields to filter. Let's name them Criteria1, Criteria2, etc (name them properly, please, not as here)

    QueryBuildRange criteria1, criteria2;
    
  • In each Datasource you need to filter, override method Init, an add code similar to this:

    super();
    criteria1 = this.query().datasource(tablenum(tableofdatasource)).addQueryRange(fieldNum(fieldtofilter))
    //criteria1.status(RangeStatus::locked); //optional - this way you can hide filter field to user, have it readonly for users, etc
    
  • Create a control of type StringEdit or ListBox in form to be used as filter. Change their AutoDeclaration property to Yes. Override modified() method. In it, I use to put something similar to:

    super();
    element.changeFilters();
    
  • In form, add method changeFilters();

    range rangeFromStringControl = StringEditControlName.text(); //Put in rangeFromStringControl the string to be used as filter, as a user would write it
    range rangeFromListBoxControl;
    criteria1.value(rangeFromStringControl);
    
    switch (listBoxControl.Selection())
    {
        case NoYesAll::All:
            rangeFromListBoxControl = ''; //Empty filter string - No filter at all
            break;
        case NoYesAll::No:
            rangeFromListBoxControl = QueryValue(NoYes::No); //Or whatever string filter value you want
            break;
        //Etc
    }
    
    //We have all filter strs done; let's filter for each main DataSource required with new filters
    DataSource1.executeQuery(); 
    //If there is other datasources joined to this one, it's usually no necessary to call their executeQuery;
    //If there are others with filters and not joined to it, call their executeQuery()
    
  • If you need this filter to be applied when form is open, set appropiate initial values to controls, and then in form's run() method:

    run();
    element.changeFilters();
    

Upvotes: 2

Related Questions