Copy Filterfields in a Report to another dataitem

ReSpongebobReSpongebob Member Posts: 17
Hi,

I would like to copy the filterfields of a dataitem in a report to another dataitem in order to prevent users having to insert the same filter multiple times for each dataitem.

I've tried to use COPYFILTERS, but that's not possible, because I have different records. The error-message indicated that I had to copy the filters one by one.

My problem is that I don't know what filterfields will be filled by the user. I also don't know how many filterfields will be entered.

Is it possible to "loop" through all the filters, and copy them one by one? :-k

Comments

  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    No, I don't think there is such a thing. Just copy the filters field by field.

    You might be able to do something with RecordRef and FieldRef, but then copying one by one is easier.
  • ReSpongebobReSpongebob Member Posts: 17
    Thanks for the quick reply.
    But I'm not that experienced in programming C/AL.

    How can I copy the filters one by one, if I don't know the fieldnames of the filters in advance? I also don't know how many filters there will be... :?
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    IF Rec1.Field1.GETFILTER <> '' then
    Rec2.Field1.SETFILTER(Rec1.Field1.GETFILTER);

    :shock:

    And then for all fields.....
  • ReSpongebobReSpongebob Member Posts: 17
    :cry: I was hoping for another answer...
    unfortunately, I fear there is indeed no other option.

    Thanks for the solution!
  • JohnJohn Member Posts: 43
    Define variable RecRef type RecordRef

    RecRef.GETTABLE("Table1");
    RecRef.SETVIEW("Table2".GETVIEW);
    "Table1".SETVIEW(RecRef.GETVIEW);

    In the case where I used it table2 is almost a copy from table1.
  • Marije_BrummelMarije_Brummel Member, Moderators Design Patterns Posts: 4,262
    John wrote:
    Define variable RecRef type RecordRef

    RecRef.GETTABLE("Table1");
    RecRef.SETVIEW("Table2".GETVIEW);
    "Table1".SETVIEW(RecRef.GETVIEW);

    In the case where I used it table2 is almost a copy from table1.

    Yes, this can work if the tables are (almost) identical, just keep in mind that the keys(sorting) are also in the view.
  • krikikriki Member, Moderator Posts: 9,086
    John wrote:
    Define variable RecRef type RecordRef

    RecRef.GETTABLE("Table1");
    RecRef.SETVIEW("Table2".GETVIEW);
    "Table1".SETVIEW(RecRef.GETVIEW);

    In the case where I used it table2 is almost a copy from table1.
    You can also loop through the fields with a fieldreference and take the filter on each field and apply it on the field in the other table.
    [/code]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • KarenhKarenh Member Posts: 209
    You can also loop through the fields with a fieldreference and take the filter on each field and apply it on the field in the other table.

    I need to do this also. I can't figure out how to do it with a "fieldreference".

    I would really appreciate some sample code.
  • krikikriki Member, Moderator Posts: 9,086
    Karenh wrote:
    I would really appreciate some sample code.

    Sorry it took so long, but I was very busy and hadn't time to see emails with replies.
    rerFrom.GETTABLE(recFrom);
    rerTo.GETTABLE(recTo);
    
    recField.RESET;
    recField.SETCURRENTKEY(TableNo,"No.");
    recField.SETRANGE(TableNo,DATABASE::Item);
    recField.SETRANGE(Enabled,TRUE);
    IF recField.FIND('-') THEN
      REPEAT
        firFrom := rerFrom.FIELD(recField."No.");
        firTo := rerTo.FIELD(recField."No.");
    
        firTo.SETFILTER(firFrom.GETFILTER);
      UNTIL recField.NEXT = 0;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DnealDneal Member Posts: 103
    rerFrom.GETTABLE(recFrom); 
    rerTo.GETTABLE(recTo); 
    
    recField.RESET; 
    recField.SETCURRENTKEY(TableNo,"No."); 
    recField.SETRANGE(TableNo,DATABASE::Item); 
    recField.SETRANGE(Enabled,TRUE); 
    IF recField.FIND('-') THEN 
      REPEAT 
        firFrom := rerFrom.FIELD(recField."No."); 
        firTo := rerTo.FIELD(recField."No."); 
    
        firTo.SETFILTER(firFrom.GETFILTER); 
      UNTIL recField.NEXT = 0;
    

    I'm trying to use this piece of code since I have the same issue. However I don't understand what some of the variables should be set as. (ie what is recField,rerFrom...?)

    Appreciate any help.

    Dneal
  • krikikriki Member, Moderator Posts: 9,086
    recFrom,recTo: some record variable
    rerFrom,rerTo : recordreference
    recField : record variable of table Field
    firFrom,firTo : fieldreference
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DnealDneal Member Posts: 103
    Hi again,

    I"m confused on what this code is doing exactly. I thought it was going to loop through all fields in one table and look for filters and put it in the second table.
       recField.SETRANGE(TableNo,DATABASE::Item);
    
        firFrom := rerFrom.FIELD(recField."No."); 
        firTo := rerTo.FIELD(recField."No.");
    

    But when I look at this I'm assuming I need to fill in the fields myself from each field to each field. Am I missing something? My apologies, I'm not that advanced with Nav programming. :?

    Dneal
  • krikikriki Member, Moderator Posts: 9,086
    I'll put some comments in it:
    rerFrom.GETTABLE(recFrom); // put the from-record in a recordreference
    rerTo.GETTABLE(recTo); // put the to-record in a recordreference
    
    recField.RESET;
    recField.SETCURRENTKEY(TableNo,"No.");
    recField.SETRANGE(TableNo,DATABASE::Item);
    recField.SETRANGE(Enabled,TRUE);
    IF recField.FIND('-') THEN
      REPEAT
        // with the SETRANGE's, the IF - THEN - REPEAT - UNTIL loops through all enabled fields of table Item
        // Each time Navision gets in this place, in recField will be the next field of table Item.
        //   (This you can check with MESSAGE('Fieldno.=%1',recField."No.");
    
        firFrom := rerFrom.FIELD(recField."No."); // point firFrom to field recField."No." in recordreference rerFrom
        firTo := rerTo.FIELD(recField."No."); // point firTo to field recField."No." in recordreference rerTo
    
        firTo.SETFILTER(firFrom.GETFILTER); // this takes the filter of firFrom and puts it in firTo
        // the first time Navision gets into the loop, this command would be the same as recFrom.SETFILTER("No.",recTo.GETFILTER("No.");
      UNTIL recField.NEXT = 0;
    
    // this last command must be added to convert recTo with all the filters in it to the record-variable.
    rerTo.SETTABLE(recTo);
    

    I hope this makes it clearer.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DnealDneal Member Posts: 103
    It's starting to make much more sense. May I ask, what do I set recField as? What do I declare it as in globals?

    This is what I have done:
    rerFrom.GETTABLE("Sales Line");
    rerTo.GETTABLE("Sales Invoice Line");
    
    recField.RESET; 
    recField.SETCURRENTKEY(TableNo,"No."); 
    recField.SETRANGE(TableNo,DATABASE::"Sales Line"); //is this correct?
    recField.SETRANGE(Enabled,TRUE); 
    IF recField.FIND('-') THEN 
      REPEAT 
        firFrom := rerFrom.FIELD(recField."No."); 
        firTo := rerTo.FIELD(recField."No."); 
    
        firTo.SETFILTER(firFrom.GETFILTER); 
      UNTIL recField.NEXT = 0;
    

    Thanks again for helping me.

    Dneal
  • krikikriki Member, Moderator Posts: 9,086
    Dneal wrote:
    It's starting to make much more sense. May I ask, what do I set recField as? What do I declare it as in globals?
    recField is a global record on table "Field".
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DnealDneal Member Posts: 103
    I'm still having issues getting this to work. When I first run it it tells me field 14010 doesn't exists in Sales Line. So I disabled it in the Sales Invoice Line just to get it to run. Of course I will need to put that back, do you know how I get past that error?
    Also once I get it to run the filters are not being copied.

    These are my dataitems.

    DataItem Name
    Customer <Customer>
    Sales Header <Sales Header>
    Sales Line <Sales Line>
    Sales Invoice Header <Sales Invoice Header>
    Sales Invoice Line <Sales Invoice Line>


    I have put the code in the onaftergetrecord of the Sales Invoice Line and then tried it on the OnPreReport. But when I debug I'm not getting the results. When it's in the OnPreReport it just copies blanks. When it's on the OnAfterGetRecord it just sets the fields. Have I put it in the right place? Perhaps this is more advanced then I'm capable of doing.

    Dneal
  • krikikriki Member, Moderator Posts: 9,086
    Try this:
    rerFrom.GETTABLE(recFrom);
    rerTo.GETTABLE(recTo);
    
    recField.RESET;
    recField.SETCURRENTKEY(TableNo,"No.");
    recField.SETRANGE(TableNo,DATABASE::Item);
    recField.SETRANGE(Enabled,TRUE);
    IF recField.FIND('-') THEN
      REPEAT
        IF rerFrom.FIELDEXIST(recField."No.") AND
           rerTo.FIELDEXIST(recField."No.") THEN BEGIN
          firFrom := rerFrom.FIELD(recField."No.");
          firTo := rerTo.FIELD(recField."No.");
    
          firTo.SETFILTER(firFrom.GETFILTER);
        END;
      UNTIL recField.NEXT = 0;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.