Parameter : Codeunit

Hi All,

I have found a function in a Codeunit where A Codeunit itself is a Parameter. Can you share your knowledge about this topic . How does it work ? Why do we need it ? :roll:

Thanks :!:

Which Codeunit?

Please post a copy of the code that call the Codeunit…

Nav2009

Codeunit 5987

Function : PostGenGnlLineSale

The function PostGenGnlLineSale is called in the codunit Serv-Documents Mgt. ( ID 5988 );

I can see that CU 5987 is called from CU5988, a number of time - please copy and paste the code you are discussing into your reply so that we can provide an explanation.

Plz see the parameters of the function “PostGenGnlLineSale”.[^o)]

Once again - please post a copy of the code where you have a question. Otherwise no one can help you. Or maybe the problem is that you don’t understandC/AL?

The PostGenGnlLineSale has a Codeunit parameter. I want to know from you why does it require ? How does it work ?

Please post a copy of the code that you are questioning - if you do not then I cannot help you.

6076.ParameterCU.JPG

The Code is copied from the CU 5988 where the function PostGnlLineSale of the CU 5987 is called. We can see the RED ticked mark is a CU and it’s passed as parameter. I just want to know from you How does it work ? And When do we requie to pass CU as parameter in a function ?

You pass a codeunit as a parameter if you want to keep the values and variables set in that codeunit across different objects and you do not want to declare it as “Single Instance”.

In the end a codeunit is an object with methods and you pass the object as a parameter to another method to make sure that it is initialized with the values necessary,

Example:

CU50000 defines a global variable “Number” as Integer and defines two functiond for SetNumber and GetNumber where the Number variable is set and retrieved as return value.

Then you define a Codeunit 50001 where you define a method which has CU50000 as Parameter. In this function you call GetNumber on the CU50000 variable and display the retrieved number.

Then you define a Codeunit 50002 where you declare CU50000 and CU50001 as a variable and call the SetNumber method with a value of 5 as example. After that you call the method in CU50001 and see what happens.

If you did everything right xou should get a display message with ‘5’.

Great[Y]