Options

Setting table filters from codeunits?

DatapacDatapac Member Posts: 97
This code works when I use it in the OnLooKup trigger of the 'No. field of a Customer tabel.

gvUser.GET(USERID);
IF gvUser."Sales Resp. Ctr. Filter" <> '' THEN BEGIN
Rec.SETCURRENTKEY("Responsibility Center");
Rec.FILTERGROUP(2);
Rec.SETFILTER("Responsibility Center", gvUser."Sales Resp. Ctr.Filter");
Rec..FILTERGROUP(0);
END;

However this filter is only applied AFTER the user does a lookup on the field. I need the filter triggered before the users even open the table.

So the question is if I want to put this code in a codeunit instead of in a table how do I reference the table data (Since I don't have a 'Rec' available)? I have tried settuping a global variable, gvCustomer that is of data type 'Record' and has a subtype 'Customer' table. So it looks like:

gvUser.GET(USERID);
IF gvUser."Sales Resp. Ctr. Filter" <> '' THEN BEGIN
gvCustomer.SETCURRENTKEY("Responsibility Center");
gvCustomer.FILTERGROUP(2);
gvCustomer.SETFILTER("Responsibility Center", gvUser."Sales Resp. Ctr. Filter");
gvCustomer.FILTERGROUP(0);
END;

However this runs error free but does not do anything.

Thanks.

Comments

  • Options
    kinekine Member Posts: 12,562
    If you want set filter to form, you can use trigger OnOpen. This trigger is called when the form is opening and the Rec is created...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    DatapacDatapac Member Posts: 97
    Yeah, we are trying to avoid setting the filter in the forms as we believe there would be a maitainence issue over time and the risk of inapropriate access for some users.

    The reason we want to set the filter in a codeunit is so we can put it in one place (e.g in the CompanyOpen function in the Application Management codeunit) where it will be applied for all users all the time no matter what form they use to access the table. Or course if it can't be done via a codeunit then we will need to look at useing forms. Thanks.
  • Options
    kinekine Member Posts: 12,562
    If you put this code into codeunit, you only need modify the form to call this codeunit and pass the Rec to this codeunit. You are not able to "catch" opening of the form to set the filters on the form without modifying the form.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    DatapacDatapac Member Posts: 97
    Thanks. via Forms it is then.
Sign In or Register to comment.