moving filters from one FILTERGROUP to another

billstollbillstoll Member Posts: 8
A form is called with some filters in FILTERGROUP(0). I don't know which fields have filters. In OnOpenForm(), I would like to move all filters to FILTERGROUP(200), as I do not want users navigating to this form to be able to change the filters that were passed in.

First thing I tried was:
myrecordref.GETTABLE(Rec);
FOR i := 1 TO myrecordref.FIELDCOUNT DO BEGIN
  myfieldref := myrecordref.FIELDINDEX(i);
  oldfilter := myfieldref.GETFILTER;
  IF myfieldref.ACTIVE AND (oldfilter <> '') THEN BEGIN
    myrecordref.FILTERGROUP(200);
    myfieldref.SETFILTER(oldfilter);
    myfieldref.FILTERGROUP(0);
  END;
END;

but unfortunately this does not affect FILTERGROUP(200) in Rec. If I could translate a fieldref back to a regular field, I could do the SETFILTER calls in Rec instead of myfieldref, but I can't figure out how to do that.

Hoping I don't have to go to all the places that call this form and set up FILTERGROUPs in them.

Thanks for any help,
Bill

Comments

  • kinekine Member Posts: 12,562
    1) You do not need to use RecordRef for that. Use just the Rec and you are done.
    2) All functions like COPYFILTERS or GETVIEW are working only with active FILTERGROUP, you can use that...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • billstollbillstoll Member Posts: 8
    Thanks for replying, Kamil.

    I am using Navision v4.0 (4.0 SP2)

    Using COPYFILTERS does not seem to work. I added this code to OnOpenForm()
    FILTERGROUP(200);
    COPYFILTERS(Rec);
    FILTERGROUP(0);
    

    Unfortunately, when user clicks "Show All", the filters are cleared. I then tried
    myrec.FILTERGROUP(200);
    myrec.COPYFILTERS(Rec);
    
    FILTERGROUP(200);
    COPYFILTERS(myrec);
    FILTERGROUP(0);
    

    "Show All" still clears the filters. Perhaps I am doing it wrong?

    If I know which fields have filters, I am all set. Suppose "Group No." has the only filter. Then I can do this:
    myfilter := GETFILTER("Group No.");
    FILTERGROUP(200);
    SETFILTER("Group No.", myfilter);
    FILTERGROUP(0);
    

    Now "Show All" does NOT clear the filter. However, to do it this way in the general case, I would have to hard code a GETFILTER call for every field in the table to check for existence of a filter and then assign it to filtergroup 200. That is what I am trying to avoid.

    Thanks,
    Bill
  • kinekine Member Posts: 12,562
    Check that GETFILTERS will return some filters before you switch the filtergroup back to 0 on the rec (e.g. by using MESSAGE). Just for check...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • billstollbillstoll Member Posts: 8
    In OnOpenForm() I added the line
    MESSAGE(GETFILTERS);
    

    at the top, and it did indeed show that two filters were set. There is no other code in OnOpenForm.

    I knew there were filters being set anyway, since when I run the form I only see one line of output. Clicking "Show All" to clear the filters results in many lines being shown. This is what I am trying to avoid by creating the filtergroup.

    So, at this point, I am still stuck with no way to copy filters to a different filtergroup except to write this block of code
    myfilter := GETFILTER(myfirstfield);
    IF myfilter <> '' THEN BEGIN
      FILTERGROUP(200);
      SETFILTER(myfirstfield, myfilter);
      FILTERGROUP(0);
    END;
    

    and repeat it for every field in the table (ugh).

    I could also parse the text output of GETFILTERS, but I don't know of a way to map a text field name to the real field in the table, so would need a giant CASE statement to do it (ugh again).

    Still seems there should be a better way.

    Thanks,
    Bill
  • matteo_montanarimatteo_montanari Member Posts: 189
    billstoll wrote:
    Thanks for replying, Kamil.

    I am using Navision v4.0 (4.0 SP2)

    Using COPYFILTERS does not seem to work. I added this code to OnOpenForm()
    FILTERGROUP(200);
    COPYFILTERS(Rec);
    FILTERGROUP(0);
    

    Unfortunately, when user clicks "Show All", the filters are cleared.

    Why did you use the filtergroup(200) instead of filtergroup(2)?
    Only filtergroup(2) cannot be removed by user.

    Matteo
    Reno Sistemi Navision Developer
  • billstollbillstoll Member Posts: 8
    Hi Matteo,
    Why did you use the filtergroup(200) instead of filtergroup(2)?
    Only filtergroup(2) cannot be removed by user.

    As I wrote earlier, the following code works fine:
    myfilter := GETFILTER("Group No.");
    FILTERGROUP(200);
    SETFILTER("Group No.", myfilter);
    FILTERGROUP(0);
    

    The user cannot clear the filter using the "Show All" button at the top of the form, despite the fact that I am using filtergroup(200) and not filtergroup(2). However, I would have to repeat that code for every field in the table.

    After getting your note, I tried
    FILTERGROUP(2);
    COPYFILTERS(Rec);
    FILTERGROUP(0);
    

    But this did not work - I was able to click "Show All" and the filters were cleared just as before. I suspect that the problem is that the above code does not actually create filters in filtergroup(2) - it does something else, not sure what.


    My bottom line question is: how to keep the user from clearing the filters in place when the form is invoked? Assuming filtergroups are the correct path, how do I use filtergroups in THIS form (e.g., how do I move filters from one filtergroup to another?) efficiently. I have ugly workarounds already (set the filtergroups in all the calling forms, change this form to make hard coded calls to SETFILTER for every field in the table that has a filter). Perhaps I must do one of those?

    Thanks,
    Bill
  • SPost29SPost29 Member Posts: 148
    This is what I did
    where Filters is a text variable

    Filters := GETVIEW;
    FILTERGROUP(24);
    SETVIEW(Filters);
    FILTERGROUP(0);
  • billstollbillstoll Member Posts: 8
    This is what I did
    where Filters is a text variable

    Filters := GETVIEW;
    FILTERGROUP(24);
    SETVIEW(Filters);
    FILTERGROUP(0);

    I just tried the above, but clicking "Show All" still clears the filters. Are you running a later version of Navision? We are running version 4 (4.0 SP2)

    Thanks,
    Bill
  • billstollbillstoll Member Posts: 8
    Check that GETFILTERS will return some filters before you switch the filtergroup back to 0 on the rec (e.g. by using MESSAGE). Just for check...

    Rereading the thread, I tried Kamil's suggestion above.
    FILTERGROUP(200);
    COPYFILTERS(Rec);
    IF CONFIRM('filtergroup 200: %1', TRUE, GETFILTERS) THEN ;
    FILTERGROUP(0);
    IF CONFIRM('filtergroup 0: %1', TRUE, GETFILTERS) THEN ;
    

    With the above code, the first CONFIRM dialog showed no filters set, and the second did show filters set. This does not look promising.

    myrec.COPYFILTERS(Rec);
    FILTERGROUP(200);
    COPYFILTERS(myrec);
    IF CONFIRM('filtergroup 200: %1', TRUE, GETFILTERS) THEN ;
    FILTERGROUP(0);
    IF CONFIRM('filtergroup 0: %1', TRUE, GETFILTERS) THEN ;
    

    With the above code, both CONFIRM dialogs showed filters set.

    Unfortunately, neither prevented "Show All" from clearing all filters. Grasping at straws, I tried inserting a RESET between the first and second lines above, but it did not change anything. Both CONFIRM dialogs still showed filters set. Surprised me a little, since the RESET should have cleared the filters in filtergroup(0). This convinces me that COPYFILTERS is not paying attention to the assigned FILTERGROUP - perhaps it assigns the filters to all groups (??), or more likely some system group that always shows up on GETFILTERS.

    Thanks,
    Bill
  • billstollbillstoll Member Posts: 8
    It looks like there is no solution to this problem.

    In case anyone is still following this thread, I decided to create FILTERGROUPs in the calling forms.

    I would still love to know if there is a real solution (how to copy filters in a form's record from one FILTERGROUP to another).

    Thanks,
    Bill
  • tylermetstylermets Member Posts: 1
    hi i used this code:
    if HASFILTER then begin
      ltxtfilters := GETVIEW;
      filtergroup(10);
      SETVIEW(ltxtfilters);
      filtergroup(0);
    end;
    

    and the users are not able to remove filters.
Sign In or Register to comment.