Is is possible to pass a variable from a form to a codeunit? I can’t understand the c.run command. Thanks.
Hi mblauer, if you want to pass the value of a form variable to code unit ,you pass as argument to codeunit ,if the function of codeunit accepts argument… Samee
Hi Guys, I think mblaur is looking to pass any variable to a codeunit. What is not clear is why so I’ll assume you want to may it available to the entire code unit. In the codeunit you can… 1) Declare myVariable as a global in the codeunit. 2) Declare a function SetMyVariable in the codeunits allow it to take a variable a as a parameter and use it to set the variable on the codeunit. 3) Do something similar to write a GetMyVariable function. On the form where you wish to access the codeunit. 1) Declare your codeunit as a variable eg lcuLibrary. 2) You can then use expressions such as lcuLibrary.SetMyVariable(xyz) and lcuLibrary.GetMyVariable. One thing to be aware of is that the code unit is NOT persistent so the values contained in it will be lost once it goes out of scope. You can get round this in at least two ways. 1) Set the codeunit to be a single instance (ATTAIN only) 2) Use a table to hold the values. Not a particularly elegant way to do things Regards Simon
Or you can do something much more simple :)… In the codeunit it self, you go (in the OnRun section) View|Properties. In the Line TableNo you set the Table. I.e. 36 (Sales Header). Now in the OnRun you could do something like this: Message(‘This order is No. %1’,“No.”); Note, “No.” do NOT need SalesHeader or something like that, because it is the implicid REC! On the Sales Order Card, create a new Menu Item, RunObject - Codeunit 50000 (Or whatever number you gave it.) By code (on that same form). Codeunit.run(“My Codeunit”,Rec); As you can see, No further globals (or locals) needed to be created! //Henrik Helgesen -: KISS::Keep it Simple, Stupid :-
Hi Henrick is right, but I would not usually use that method to call a codeunit. The only time you would really call codeunit.RUN is if you do not want run time errors to occur and will trap/ignore the error. If a run time error occurs in a code unit then c.RUN will return False else it returns true. As you are running the codeunit from a form you will want run time error to show. This is normally the case. In you codeunit create a sub routine and the define the parameters you want to pass in the local variables. Now all you do is define a variable as type codeunit. Then call codeunit.functionname(Var1,Var2,Var3). You can pass any datatypes as variables and also pass the variables as var type. You can define procedures to be local to the codeunit so they can not be seen outside the codeunit. You can then gather all similar subroutines into a single codeunit to allow for code reuse. This is what Navision actually does except for the posting function. Paul Baxter
Hi Guys, I think we really need to know what mblauer is trying to achieve… If he simply wants to pass a record then Henrik is correct in that setting the property is easy but I do share Paul’s reservations. Also, it is not easy to pass simple variables in this way. Also, Paul is correct in terms of data hiding etc to pass all variable as parameters to functions — i hate those darned globals — but it may not be feasible dependent on what the requirements are. I think MBlauer needs to elaborate!!! Regards Simon
Originally posted by triff: Hi Henrik is right, but I would not usually use that method to call a codeunit. The only time you would really call codeunit.RUN is if you do not want run time errors to occur and will trap/ignore the error. If a run time error occurs in a code unit then c.RUN will return False else it returns true.
Paul - So far we agree, However, if you do a C.run you WILL get error messages!, Only if you do a IF C.Run you will awiod messages! Further details - See Posting of a Sales Header. Post does a C.RUN, whereas the Mass Posting does a IF C.RUN… //Henrik Helgesen -: KISS::Keep it Simple, Stupid :-
Hi Yes I know, so why use C.Run and not C.PROC(Var1,…) which will allow you to have more than one entry point to the codeunit and pass more than a single table. this allows you to gather all the simular procedures into one codeunit. Paul Baxter
Paul - I personally use both C.RUN(REC) and MyCodeunit.MyFunction(Var1,Var2,…); When do I use one in stead of the other? I generally use C.RUN(REC) when calling from a form. Primarally because I think it’s more clean, as you do not need to add variables on the form. But, of cource, if I need values that are not part of the table, I use MyCodeunit.MyFunction(Var1,Var2,…); I would say, that I proberbly uses MyCodeunit.MyFunction(Var1,Var2,…) 9 time out of 10. I’m not saying that I think it is wrong to use it //Henrik Helgesen -: KISS::Keep it Simple, Stupid :-