D365 Business Central - How To Work With Codeunit Object!
- 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 details kindly visit our LinkedIn page:
https://www.linkedin.com/company/allgrow-technologies-pvt-ltd/mycompany/
or you can write us on contact@allgrowtech.com
DO YOU WANT TO TURN YOUR IDEA INTO REALITY
Services we offer
Consulting Services
Software and Web Development
Development
Frontend
Backend
DevOps
SAAS
Locate us at
Karnataka
My Office Space, No 22/B, 2nd Floor,
BM Habitat Mall, Gokulam Main Road,
Jalakashmipuram, Mysore-570012,
Karnataka, India.
+91 9538770892
Uttar Pradesh
4th Floor, Bhavya Corporate Tower,
Vibhuti Khand, Gomti Nagar, Lucknow,
Uttar Pradesh 226010
+91 726-804-5173
Get in Touch
+1 646-432-0784
+91 9538770892
For Business – contact@allgrowtech.com
For Career – hr@allgrowtech.com
ALLGROW TECHNOLOGIES
We are here to provide quality technical solutions that perfectly align with your business requirements.