pass record as a parameter

boubounasboubounas Member Posts: 17
Hi,

Since I couldnt find the answer I was looking for on the other posts with similar title I thought of creating a new one.

I have a record which I do some calculations with it in a codeunit. After my calculations the record has, say, COUNT = 3. If I pass this record to a form in the following way everything is fine:
MyForm.SETRECORD(MyRec);
MyForm.RUNMODAL();

In the form now the MyRec has COUNT = 1.

However if I do:
FORM.RUNMODAL(FORM::MyForm, MyRec);

Then the form displays all 3 records.

Note that the MyRec variable is a parameter to a function in the codeunit. In this function the calculations are being done and the form is being run. The VAR check box is checked in the MyRec parameter and the codeunit is SingleInstance = NO.

Can someone please explain me the difference and how can I use the first way and make it work?

Thanks in advance.
"After 5 billion years man said: Let there be God"

Answers

  • kinekine Member Posts: 12,562
    Try to use SETRECFILTER on the record variable to set filter to just the one actual record.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • boubounasboubounas Member Posts: 17
    kine wrote:
    Try to use SETRECFILTER on the record variable to set filter to just the one actual record.

    I dont have any filters set on this record (MyRec). I even do a RESET just before I pass it in the form (after the RESET the COUNT is 3). :-k
    "After 5 billion years man said: Let there be God"
  • Saint-SageSaint-Sage Member Posts: 92
    So basically you want to be able to do your .SETRECORD(Rec) and have it intialize the variables for you before you open the form, however when you do that you only get the current record instead of all three...

    At least that is what I think you are saying..


    What does the code inside your SETRECORD function do?

    It should look something like...

    SETRECORD(inputRecord)

    globalFormRecord := inputRecord;

    And you shouldn't have to pass by reference, by value should be fine...

    No one loves you like the one who created you...
  • boubounasboubounas Member Posts: 17
    Saint-Sage wrote:
    What does the code inside your SETRECORD function do?

    Yes you understand corectly what I want to do.

    The only difference is that the SETRECORD is not my function. Is the standard C/AL function to pass a record in a form. I dont know what it does, its not visible in the form.

    Thanks for the reply
    "After 5 billion years man said: Let there be God"
  • Saint-SageSaint-Sage Member Posts: 92
    In that case just create a function yourself similar to what I had up there before.


    I.E. on the form create a function called
    setCurrentWhateverRec(inputRec)
    
    and do the assignment within the function
    GlobalFormVariableRec := inputRec;
    

    Now you have a way to set the current record you wish to calculate before you run the form.

    Since you are doing a runmodal, you can even retrieve response or return variables from the form before it is destroyed by creating a get function as well. This is usually how I handle passing variables between forms.

    No one loves you like the one who created you...
  • boubounasboubounas Member Posts: 17
    I have tried this already, tried it again just now and it didnt work. In fact the GlobalFormVariableRec.COUNT = 0 inside the setCurrentWhateverRec(inputRec) function in your mentioned code. However, the inputRec.COUNT = 3.

    Here is the function:
    setCurrentWhateverRec(VAR inputRec : Record "MyRec")
    {
      GlobalFormVariableRec := inputRec;
     //at this point GlobalFormVariableRec.COUNT is 0
     //but inputRec.COUNT is 3!
    
     //I also tried GlobalFormVariableRec.COPY(inputRec)
     //and got the same result.
    }
    
    
    ](*,)
    "After 5 billion years man said: Let there be God"
  • kinekine Member Posts: 12,562
    1) SETRECFILTER set filter in a way that just the current record will be inside the filter
    2) SETRECORD is passing filters from your variable to the form
    3) If you reset all fitlers, you will see all in your form...
    4) If you use SETRECFILTER, you will see just the actual record in the form
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • boubounasboubounas Member Posts: 17
    kine wrote:
    1) SETRECFILTER set filter in a way that just the current record will be inside the filter
    2) SETRECORD is passing filters from your variable to the form
    3) If you reset all fitlers, you will see all in your form...
    4) If you use SETRECFILTER, you will see just the actual record in the form

    Kine, Thanks for your explaination. Unfortunately, I had all those in mind and still I must be doing something wrong. Here is again my code:

    1st way:
    //In my Codeunit:
    
      //Calculations of MyRec
    
      MyRec.RESET;
      //MyRec.COUNT equals 3 at this point!
    
      MyForm.SetMyRec(MyRec); // I have also tried MyForm.SETRECORD(MyRec);
      MyForm.RUNMODAL();
    
    //In MyForm C/AL
    SetMyRec(VAR MyRec)
    {
      //MyRec.COUNT equals 3 at this point!
    
      Rec.COPY(MyRec);
    
      //Rec.COUNT equals 1 at this point! WHY???
    }
    

    2nd Way
    //In my Codeunit:
      FORM.RUNMODAL(FORM::MYFORM, MyRec);
    

    The 2nd way displays all 3 records in the form unlike the 1st way.

    Thanks[/b]
    "After 5 billion years man said: Let there be God"
  • kinekine Member Posts: 12,562
    1) There is something strange:

    in your code you have:
      MyForm.SetMyRec(MyRec); 
      MyForm.RUNMODAL();
    

    and than you wrote, that you are using
    FORM.RUNMODAL(FORM::MYFORM, MyRec); 
    

    Why? The first RUNMODAL is enough to run the form. The second command run the same form, but on MyRec which has no filters (you did reset before). It is not affected by function SetMyRec called before...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • boubounasboubounas Member Posts: 17
    kine wrote:
    1) There is something strange:

    in your code you have:
      MyForm.SetMyRec(MyRec); 
      MyForm.RUNMODAL();
    

    and than you wrote, that you are using
    FORM.RUNMODAL(FORM::MYFORM, MyRec); 
    

    Why? The first RUNMODAL is enough to run the form. The second command run the same form, but on MyRec which has no filters (you did reset before). It is not affected by function SetMyRec called before...

    Kine,

    The bit you didnt understand is that I have 2 DIFFERENT WAYS of running the form. Please see previous post for 1st way and 2nd way. The 1st DOESNT work and the 2nd WORKS! I dont use both ways at the same time obviously.

    Thanks
    "After 5 billion years man said: Let there be God"
  • kinekine Member Posts: 12,562
    I recommend to turn on debugger and check filters on the records in hot spots (after setting filters, after copy of filters etc.). Do not forget to check other filtergroups than 0 if there are some filters (you will need to do it in code, debugger is not able to show that).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • boubounasboubounas Member Posts: 17
    Kine,

    I mentioned before that there are no filters in MyRec when I pass it in as a parameter. I do check it with the MyRec.HASFILTER() function before I pass it to the form and also from inside the form C/AL code I check it again. On top of that, just to make sure, I do a MyRec.RESET just before I pass it in the form as you can see in previous posts above. ](*,)
    "After 5 billion years man said: Let there be God"
  • boubounasboubounas Member Posts: 17
    "After 5 billion years man said: Let there be God"
Sign In or Register to comment.