Export (0) Print
Expand All

Walkthrough: Using Codeunits

Microsoft Dynamics Nav 2009

You can treat codeunits as objects. One codeunit variable can be assigned to another codeunit variable, which creates a new reference to the same codeunit instance. The codeunit variables then use the same set of internal variables.

This walkthough illustrates the following tasks:

  • Creating one codeunit that has two functions: Set and Get. Set sets an internal variable to the value of the parameter given. Get returns the value of the internal variable.

  • Creating one codeunit that has two variables that call the first codeunit and are then assigned to each other so that they use the same instance of the first codeunit.

  • Modifying one of these codeunits to make it a single instance codeunit.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV installed with a developer license

  • The CRONUS International Ltd. demo data company installed

Viktor is a Microsoft Certified Partner working for CRONUS International. To learn about codeunits in Microsoft Dynamics NAV, he plans to create some codeunits and use variables in those codeunits in different ways.

First, you must create a codeunit that includes the Set and Get functions. You must also write the code for these two functions.

To create the first codeunit

  1. In the Classic client, click the Tools menu, and then click Object Designer.

  2. In Object Designer, click Codeunit and then click New.

  3. In the View menu, click C/AL Globals.

  4. On the Variables tab, in the Name field, enter InternalInt to create a new variable. In the DataType field, select Integer from the drop-down list.

  5. In the C/AL GLobals window, click the Functions tab.

  6. On the Functions tab, in the Name field, add two functions called Set and Get.

  7. Select the Set function and then click Locals.

  8. In the Set - C/AL Locals window, on the Parameters tab, in the Name field, enter input. In the DataType field, select Integer from the drop-down list.

  9. Close the Set - C/AL Locals window.

  10. In the C/AL GLobals window, select the Get function, and then click Locals.

  11. In the Get - C/AL Locals window, click the Return Value tab.

  12. In the Name field, enter out. In the Return Type field, select Integer from the drop-down list.

  13. Close the Get - C/AL Locals window.

  14. In the C/AL Editor for the codeunit, in the Set function section, enter the following code.

    InternalInt := input + 1;
    Get;
    

    The code in the Set function increases the value of the input by one and sets this as the value of the internal variable. It then calls the Get function.

  15. In the C/AL Editor, in the Get function section, enter the following code.

    out := InternalInt;
    

    The code in the Get function specifies that the new value of the internal variable is the output value generated by this codeunit.

  16. Close the C/AL Editor. Click Yes to save your changes to the codeunit.

  17. In the Save As window, in the ID field, enter 70000. In the Name field, enter Set_Get. Select the Compile check box, and then click OK.

The next step is to create the codeunit that calls two instances of the Set_Get codeunit and supplies the input values that these instances should use. This codeunit contains two variables, which are assigned to each other so that they both use the same instance of codeunit 70000.

To create the second codeunit

  1. In Object Designer, click Codeunit, and then click New.

  2. In the View menu, select C/AL Globals.

  3. On the Variables tab, in the Name field, enter CoMIC1 to create a new variable. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70000, Set_Get.

  4. In the C/AL GLobals window, on the Variables tab, in the Name field, enter CoMIC2 to create a second variable. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70000, Set_Get.

  5. Close the C/AL GLobals window.

  6. In the C/AL Editor, add the following code to the OnRun trigger.

    CoMIC1.Set(1);
    CoMIC2.Set(2);
    // Calls two instances of codeunit 70000 
    // with the input parameters 1 and 2, respectively.
    MESSAGE('When running two instances, the value of CoMIC1 is %1 and the value of CoMIC2 is %2.',CoMIC1.Get,CoMIC2.Get);
    // The message retrieves the return values from codeunit 70000 
    // and shows them in the message.
    // CoMIC2 returns 2. 
    // ComIC2 returns 3, because you have added 1 to each parameter.
    CoMIC2 := CoMIC1;
    // CoMIC2 is assigned to CoMIC1 
    // and they both use the same instance of codeunit 70000.
    MESSAGE('After assinging CoMIC2 to CoMIC1, the value of CoMIC1 is %1 and the value of CoMIC2 is also %2.',CoMIC1.Get,CoMIC2.Get);
    // CoMIC1 and CoMIC2 now show the same value.
    

    The first statement in this codeunit calls two instances of the Set_Get codeunit and supplies the input values that these instances should use. The next statement assigns the two variables to each other so that they both use the same instance of codeunit 70000 and therefore generate the same return value.

  7. Close the C/AL Editor. Click Yes to save your changes to the codeunit.

  8. In the Save As window, in the ID field, enter 70001. In the Name field, enter Assignment. Select the Compile check box, and then click OK.

In some cases, only one instance of a codeunit needs to exist. This means that all the codeunit variables of a particular codeunit use the same set of variables. You can make a codeunit a single instance codeunit by setting the SingleInstance property to Yes.

To modify a codeunit to a single instance codeunit

  1. In Object Designer, click Codeunit, select codeunit 70000, and then click Design.

  2. In the View menu, click Properties.

  3. In the Properties window, in the SingleInstance field, select Yes from the drop-down list.

  4. Close the Set_Get - Properties window.

  5. In the File menu, click Save As.

  6. In the Save As window, in the ID field, enter 70002. In the Name field, enter SingleInst. Select the Compile check box, and then click OK.

  7. In Object Designer, click Codeunit, select codeunit 70000, and then click Design.

  8. In the View menu, select C/AL Globals.

  9. On the Variables tab, in the Name field, enter CoMIC1. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70002, SingleInst.

  10. On the Variables tab, in the Name field, enter CoMIC2. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70002, SingleInst.

  11. Close the C/AL Globals window.

  12. In the C/AL Editor, add the following code to codeunit 70000 to the OnRun trigger:

    CoMIC1.Set(7);
    // A codeunit instance is created if one did not exist.
    CoMIC2.Get();
    // Returns 8, that is 7 + the 1 added by the set function in 
    // codeunit 70002. CoMIC2 uses the same instance as CoMIC1, 
    // they use the same internal variables.
    MESSAGE('the value of CoMIC2 is %1',CoMIC2.Get);
    
  13. Close the C/AL Editor. In the Save Changes window, select the Compiled check box, and then click Yes to save your changes.

  14. In Object Designer, select codeunit 70000, and then click Run. The following message appears, "The value of CoMIC2 is 8"

    The input value in codeunit 70000 is 7 and codeunit 70002 adds 1 to this value.

  15. In Object Designer, select codeunit 70002, and then click Design.

  16. In the Set function, change the first line to the following.

    InternalInt := input+4.

  17. Close the C/AL Editor. In the Save Changes, window, select the Compiled check box, and then click Yes to save your changes.

  18. In Object Designer, select codeunit 70000, and then click Run. You will get the same message that you received in step 14. This occurs because after you have loaded a single instance codeunit, it remains loaded on the client until you restart the client.

    NoteNote

    It is possible to use a single instance codeunit across objects, not just within the same object.

Community Additions

ADD
Show:
© 2014 Microsoft