Codeunit definition

A codeunit is a container of AL code, and this code can be triggered by directly executing the codeunit (with the OnRun trigger) or by calling the functions defined in the codeunit itself.

We can define a codeunit in AL by using the tcodeunit snippet:

codeunit Id MyCodeunit
{
trigger OnRun()
begin

end;

var
myInt: Integer;
}

A codeunit is defined by an ID and a name (which must be unique inside your application). By default, the codeunit skeleton only contains the OnRun trigger definition, and inside this trigger, you can write the code that you want to execute when calling the Codeunit.RUN method.

A codeunit has its own properties that you can set:

In a codeunit, you can define procedures (functions) that can be local to the codeunit or global (that is, publicly exposed to objects that instantiate the codeunit).

A procedure can be defined by using the tprocedure snippet:

local procedure MyProcedure()
var
myInt: Integer;
begin

end;

By default, this snippet creates a local procedure without parameters and without a return value. You can change the scope from local (the default value, meaning that it is visible only inside the object that declares the procedure) to global (so that it is also visible outside the object) by removing the local keyword.

As an example, this is a global procedure with parameters and a return value:

procedure CheckIfPacktCustomerIsEnabled(CustomerNo: Code[20]): Boolean
var
//Local variables here
begin
//Method code here
end;

A codeunit can have more than one procedure (local or global) defined.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.125