Persisting Filter conditions in EP Grid


Persisting Filter condition in EP Grid


In AX 2009, EP grid control comes with an advanced filter. Here is a code to persist the filter condition the user last entered in a page for that user, so that later when the user comes back to the same page the last filter is applied and user didn’t have to reenter then.


This code sample assumes a dataset with the name “EPSalesTableList” which contains the datasource “SalesTable”


 (1) First override the pack method of the dataset ( EPSalesTableList in this example) in AOT


public container pack()
{


container ret;
SysLastValue sysLastValue;


formdatasource filterDataSource;
;


filterDataSource = SalesTable_ds; // Put your datasource name


    ttsbegin;
       // Delete last saved query for the current dataset
    delete_from sysLastValue
        where sysLastValue.Company      == curext()
           && sysLastValue.UserId       == curuserid()
           && sysLastValue.RecordType   == UtilElementType::DataSet
           && sysLastValue.ElementName  == filterDataSource.name()
           && sysLastValue.DesignName   == filterDataSource.name();


    // If there is a new queryRun() object then serialize and save it
    // in the sys last value table
    // Put your datasource name
   
    if (filterDataSource.queryRun())
   {
   
        sysLastValue.RecId = 0;
        sysLastValue.Company      = curext();
        sysLastValue.UserId       = curuserid();
        sysLastValue.RecordType   = UtilElementType::DataSet;
        sysLastValue.ElementName  = filterDataSource.name();
        sysLastValue.DesignName   = filterDataSource.name();
        sysLastValue.value = SysQuery::packRangeAndSortorder(filterDataSource.queryRun().query());   
        sysLastValue.insert();
    }
    ttscommit;


 


    ret = super();


 


 


    return ret;


}


 


 


(2) Second override the init method of the datasource ( SalesTable in this example) within that dataset in AOT


public void init()
{
SysLastValue    sysLastValue;
    Query savedQuery;
    ;
    super();


// get the last value from the  sys last value table
    select firstonly sysLastValue
        where sysLastValue.Company      == curext()
           && sysLastValue.UserId       == curuserid()
           && sysLastValue.RecordType   == UtilElementType::DataSet
           && sysLastValue.ElementName  == this.name()
           && sysLastValue.DesignName   == this.name();


    if (sysLastValue && sysLastValue.Value)
    {
              // If there is an unpack error delete the saved query


       if (!SysQuery::unpackRangeAndSortorder(this.query(), sysLastValue.value))
        {


            ttsbegin;
            delete_from sysLastValue
                where sysLastValue.Company      == curext()
                   && sysLastValue.UserId       == curuserid()
                   && sysLastValue.RecordType   == UtilElementType::DataSet
                   && sysLastValue.ElementName  == this.name()
                   && sysLastValue.DesignName   == this.name();
            ttscommit;
        }


    }
               


 


 Make sure you replace the datasourcename variable filterDataSource in the pack method with your datasource name.Save the changes, refresh AOD and now go to the page that is using this dataset and enter some filter condition & apply. Close the browser and reopen the page. Now you should see that the filter that you last applied before the browser was closed is still applied in this page.


Arif Kureshy, who is the dev manager for EP came out with this code snippet. So thanks to him for providing this sample code.


 

Comments (11)

  1. player says:

    Thanks for this code snippet. But more interest is how to make user’s choice from some preffered filters not the only last.

  2. Pelle Portal says:

    We are running DAX 2009 in Norwegian Language, and the following error occurs (Translated via Google translator)"Error code execution: Wrong argument type in variable assignment.  (C)Data setsEPInventTableListMethodsunpack"

    Here’s the code in unpack:

    public boolean unpack(container _pack)

    {

       Integer     version = conpeek(_pack,1);

       container   base;

       boolean     ret;

       ;

       switch (version)

       {

           case #CurrentVersion    :   [version, #currentList, base] = _pack;

                                       ret = super(base);

                                       break;

           default: super(_pack);

       }

       return ret;

    }

    This version is updated with Windows Update, but no DAX System Update is done since August 2009.

    This error does not occur in an English version which has not updated since August 2009. Any ideas?

  3. Roberto says:

    Is there any possibility to preset the filter condition (Field + type) on the item lookup on the user control that create the sales order?

    My customer is asking this implementation but i don’t know how to do it.

    Thank you

  4. Bill says:

    Hi,

     This works great when I have another form to call for editing, so when the user comes back they have their original query.  How can I handle when it’s a new session, client wants to ensure the filter is refreshed.  Appreciate the help.

    Bill

  5. Abhishek says:

    HI,

    I am trying to modify the EP filter control,

    How I can do that,

    In PSA module the Manage Timesheet page:

    When I filter it for status I can see and edit other users  timesheet as well

    Please help me the some solution

    Thank you

    Abhishek Mehta

  6. Mey M says:

    Make sure you are not in admin usergroup and the right employee id is assigned to the Ax user account using User relations

  7. Mike says:

    Hi Mey,

    I have a situation where the customer would like to have default fields shown within the filter, since they use these fields all the time as range. If I add ranges on the query like

    this.query().dataSourceTable(tablenum(CustTable)).addRange(fieldnum(CustTable,AccountNum));

    is the field not shown. The range need a range value to show (like '*'). Is there a way to get around this?

    A complete other issue. If I use the AXDatePicker control and do the following make the web site "hang":

    1. Click on calendar to open calendar lookup

    2. Don't select any date (click beside the lookup)

    3. Now click on calendar again and you get "error on page".

    Thank you

    Michael

  8. Mey says:

    for the DatePicker hang issue, plesae contact support and ask for hotfix.

    for the std filter, there is no way around to avoid specifying a value when added through the code.

    The other option is to build a web control with the fields that you want to filter on ( based on EDTs) and put it at top of the grid user control and pass the selected values to the user control and add the filter through managed code.

  9. Fradim9 says:

    Many thanks for your articles, we've fixed a lot of issues in our EP 🙂

    Just one question about current topic. It works fine if user close and reopen browser. But let's imagine a little bit different situation. User applied some filters, had some entries/documents in Grid and went through one of the documents from Grid (just clicked on the link). After looking the document he just pushed browser's Back button and… returned to previous page without applied filters 🙁 Is there the way to fix it? I mean, apply previous filters even the user just pushed browser's Back button, not reopen Browser?

    We've found Michael Nilsson' site http://ajaxhistory.com/ with general suggestion, should we go this way, or there is different solution especially for EP?

  10. Fradim9 says:

    Many thanks for your articles, we've fixed a lot of issues in our EP 🙂

    Just one question about current topic. It works fine if user close and reopen browser. But let's imagine a little bit different situation. User applied some filters, had some entries/documents in Grid and went through one of the documents from Grid (just clicked on the link). After looking the document he just pushed browser's Back button and… returned to previous page without applied filters 🙁 Is there the way to fix it? I mean, apply previous filters even the user just pushed browser's Back button, not reopen Browser?

    We've found Michael Nilsson' site http://ajaxhistory.com/ with general suggestion, should we go this way, or there is different solution especially for EP?

  11. Arun says:

    Hi,

    I am not able to save the value on User Control which contain group.

    Regards

    Arun Garg

Skip to main content