Create and access a codeunit
To create a new codeunit, you can use the tcodeunit snippet. Make sure that you provide a number and a name for your codeunit. This step will help you access the functions within the codeunit by name.
codeunit 50100 MyCodeunit
{
Access = Public;
Subtype = Normal;
trigger OnRun()
begin
end;
procedure MyFunction(Param1: Integer; Param2: Text[50]) : Boolean
begin
end;
}
To access functions within a codeunit, first create a variable of type Codeunit. Then, you can access all codeunit functions (depending on their access properties).
codeunit 50101 MyCodeunit2
{
trigger OnRun()
var
MyCodeUnit1: Codeunit MyCodeunit;
Result: Boolean;
begin
Result := MyCodeUnit1.MyFunction(5, 'Test');
end;
}
You can also access a codeunit from within a page by using the RunObject property on an action.
If you use the RunObject property, you can only run the OnRun trigger, not the other functions within the codeunit.
actions
{
area(Processing)
{
action(ActionName)
{
ApplicationArea = All;
Image = NewSum;
Caption = 'ActionName';
ToolTip = 'Click to run MyCodeunit';
RunObject = codeunit MyCodeunit;
}
}
}
To access the other functions, you can use the OnAction trigger.
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;
}
}
}