- A codeunit is a container for AL code that we can use in many application objects.
- We implement business logic in codeunits and call that particular codeunit from the object that needs to perform that specific logic or action.
Basically codeunit can be used in following way:
- First we create a codeunit snippet by using key word ‘tcodeunit’
- After that we will give the proper ID number and name
- In the procedure section we assign a proper name and write our logic in this section
For example:-
codeunit 50101 “Test Functions”
{
procedure Factorial()
var
IntValue, IntValue2, Fact : Integer;
begin
IntValue := 5;
IntValue2 := IntValue;
Fact := 1;
while
IntValue >= 1
do begin
Fact := Fact * IntValue;
IntValue -= 1;
end;
Message(‘IntValue = %2, My Factorial value is %1’, Fact, IntValue2);
end;
}
- The next step is to decide in which page we should see our desired result
- So we will create a ‘page extension’ for that page using snipper ‘tpageext’
- Inside the action section we give our action a name and call our code unit using a trigger
For example :-
pageextension 50101 CustomerListExt extends “Item List”
{
layout
{
// Add changes to page layout here
}
actions
{
addlast(Functions)
{
action(Factorial)
{
ApplicationArea = All;
Caption = ‘Factorial’;
trigger OnAction()
var
TestFunctionCodeunit: Codeunit “Test Functions”;
begin
// TestFunctionCodeunit.Run();
TestFunctionCodeunit.Factorial();
end;
}
}
After this, we should publish our program and check for the result
Advantages of CodeUnit :-
- CodeUnit is the best way for creating procedures
- We can simply call those codeunits wherever we wish in our program and this help in reusability .
For more information like this please follow our company LinkedIn page:
https://www.linkedin.com/company/allgrow-technologies-pvt-ltd/
By Vinyas S.