Créer et accéder à un codeunit

Effectué

Pour créer un codeunit, vous pouvez utiliser l’extrait de code tcodeunit. Assurez-vous de fournir un numéro et un nom pour votre codeunit. Cette étape vous aidera à accéder aux fonctions du codeunit par nom.

codeunit 50100 MyCodeunit
{
    Access = Public;
    Subtype = Normal;

    trigger OnRun()
    begin
        
    end;

    procedure MyFunction(Param1: Integer; Param2: Text[50]) : Boolean
    begin 

    end;
}

Pour accéder aux fonctions au sein d’un codeunit, créez d’abord une variable de type Codeunit. Ensuite, vous pouvez accéder à toutes les fonctions codeunit (en fonction de leurs propriétés d’accès).

codeunit 50101 MyCodeunit2
{
    trigger OnRun()
    var
        MyCodeUnit1: Codeunit MyCodeunit;
        Result: Boolean;
    begin
        Result := MyCodeUnit1.MyFunction(5, 'Test');
    end;
}

Vous pouvez également accéder à un codeunit à partir d’une page en utilisant la propriété RunObject sur une action.

Si vous utilisez la propriété RunObject, vous ne pouvez exécuter que le déclencheur OnRun, pas les autres fonctions du codeunit.

actions
{
    area(Processing)
    {
        action(ActionName)
        {
            ApplicationArea = All;
            Image = NewSum;
            Caption = 'ActionName';
            ToolTip = 'Click to run MyCodeunit';
            RunObject = codeunit MyCodeunit; 
        }
    }
}

Pour accéder aux autres fonctions, vous pouvez utiliser le déclencheur OnAction.

actions
{
    area(Processing)
    {
        action(ActionName)
        {
            ApplicationArea = All;
            Image = NewSum;
            Caption = 'ActionName2';
            ToolTip = 'Click to run MyCodeunit';
            RunObject = codeunit MyCodeunit; 
            trigger OnAction()
            var 
                MyCodeunit1: Codeunit MyCodeunit;
            begin 
                MyCodeunit1.MyFunction(2, 'Test 2');
            end;
        }
    }
}