- 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
codeunit 50101 “Test Functions”
IntValue, IntValue2, Fact : Integer;
IntValue := 5;
IntValue2 := IntValue;
Fact := 1;
IntValue >= 1
Fact := Fact * IntValue;
IntValue -= 1;
Message(‘IntValue = %2, My Factorial value is %1’, Fact, IntValue2);
- 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”
// Add changes to page layout here
ApplicationArea = All;
Caption = ‘Factorial’;
TestFunctionCodeunit: Codeunit “Test Functions”;
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:
By Vinyas S.