filter Rec using fieldref

dsatria
dsatria Member Posts: 80
How do we put filter on Item Card form using C/AL code?

I tried this but not work:
lRecRef.gettable(rec);
lfldref:= lrecref.field(fieldno("Vendor No."));
lfldref.setfilter('10-0085');
lrecref.settable(rec)

My plan is to make a dynamic filter based on field/value defined on other table.

Comments

  • pduck
    pduck Member Posts: 147
    Before SETTABLE you must process a classical RecRef.FIND('-') so that the RecRef points to the filtered Rec using the Filters on its referenced FieldRefs. After that you can do the SETTABLE to repoint your original Rec on RecRefs Position.
  • dsatria
    dsatria Member Posts: 80
    pduck,

    I changed the code to
    lrecref.GETTABLE(Rec);
    lfldref:= lrecref.FIELD(FIELDNO("Vendor No."));
    lfldref.SETFILTER('10-0085');
    lrecref.FIND('-');                   <--- added
    lrecref.settable(rec);
    

    but still not working

    I tried two different places:
    1) OnInit: first record matches filter displayed but subsequent records ignore the filter
    2) OnAfterGetRecord: can't move to another records except the first matching one

    any other suggestions?
  • pduck
    pduck Member Posts: 147
    Does it work when you replace SETFILTER WITH SETRANGE? Maybe it needs a full qualified FilterString like "= 'XYZ'" when using SETFILTER.
  • Dduran
    Dduran Member Posts: 22
    I propose you anther way to do the same.

    Table.OPEN(27) //Item
    lfldref:= lrecref.field(fieldno("Vendor No."));
    table.SETVIEW(' WHERE (' + lfldref.CAPTION + ' = CONST('10-0085'))');

    IF table.FINDFIRST THEN
    repeat
    ---
    ----
    until (table.next=0)

    Regards
  • dsatria
    dsatria Member Posts: 80
    @pduck:
    I tried with SETRANGE and the result was exactly the same with using SETFILTER

    @dduran:
    I'm NOT trying to scan a table using certain filters.
    I'm trying to filter SourceTable of CURRENT FORM using dynamic criterion
  • dsatria
    dsatria Member Posts: 80
    I just tried using SETVIEW...the filter worked as expected...but with one drawback: user can see the filter and change it!
    Too bad we can't combine SETVIEW with FILTERGROUP :(

    Any inputs?
  • David_Cox
    David_Cox Member Posts: 509
    dsatria wrote:
    How do we put filter on Item Card form using C/AL code?

    I tried this but not work:
    lRecRef.gettable(rec);
    lfldref:= lrecref.field(fieldno("Vendor No."));
    lfldref.setfilter('10-0085');
    lrecref.settable(rec)
    

    My plan is to make a dynamic filter based on field/value defined on other table.

    The problem might be the code on the "Item Card" form, which clears the filters on after get record

    Form - OnAfterGetRecord()
    SETRANGE("No.");
    ItemCostMgt.CalculateAverageCost(Rec,AverageCostLCY,AverageCostACY);
    EnablePlanningControls;
    Analyst Developer with over 17 years Navision, Contract Status - Busy
    Mobile: +44(0)7854 842801
    Email: david.cox@adeptris.com
    Twitter: https://twitter.com/Adeptris
    Website: http://www.adeptris.com
  • dsatria
    dsatria Member Posts: 80
    David Cox wrote:
    The problem might be the code on the "Item Card" form, which clears the filters on after get record

    Form - OnAfterGetRecord()
    SETRANGE("No.");

    I think so...but what's that code for anyway? Will remove the line affect navision somehow?