Apex programming has become the backbone of many business-critical apps running on Salesforce CRM.
And if you’re reading this, chances are that you’re completely new to Apex, wondering where to start.
Unlike the typical “hello world” tutorials, we’re going hands-on with real code, giving you a better idea of the language.
So, let’s get started.
What is Apex in Salesforce?
Apex is a server-side programming language developed by Salesforce. It is similar to Java that runs inside the Salesforce ecosystem with its own set of rules and limits.
The typical use case of the language included:
- It automates complex business logic by creating triggers.
- Integrates Salesforce with external systems (like e-commerce, accounting, ERP, and more).
- It processes large datasets securely within the Salesforce platform.
Data Types in Apex
The Apex programming language is strongly typed, so it is recommended to properly assign values to variables.
| Data Type | About | Value |
| Integer | It represents numbers that don’t include any decimal point. | 2, 10, 100 |
| Boolean | Logical value to identify a condition | True, False, or Null |
| Decimal | It represents a number with a decimal point | 23.5, 225.45, 2456.65 |
| Date | The variable indicates a date | 2026/02/09 |
| String | It denotes characters inside single quotes | ‘Xyz Technologies’ |
Collection in Apex
Apex gives you three main collection types, and knowing when to use each one will save you from writing sloppy code.
- Lists are your go-to for ordered data
- Sets eliminate duplicates automatically:
- Maps are key-value storage used for constant performance
Salesforce Apex Code Examples
1. For Display the Simple Text and Integer
Integer A = 123;
String B = 'Salesforce';
System.debug('Value is =' + A);
System.debug('String is =' + B);
Output
Value is =123
String is =Salesforce
2. Check whether the number is even or not
Integer n= 3;
Integer r;
r = System.Math.mod(n, 2);
if(r == 0)
{
System.debug('The no is Even');
} else
{
System.debug('The no is odd');
}
Output
The no is odd
3. To find the Biggest 3 no.
integer x=1, y=-66, z=44;
if (x > y)
{
if(x > z)
{
System.debug(x+ ' x is the Big');
} else
{
System.debug(z+ ' z is the big');
}
} else
{
if(y > z)
{
System.debug(y+ ' y is the big');
} else
{
System.debug(z+ ' z is the big');
}
}
Output
44 z is the big
4. While Loop Example
Integer deptt = 1;
while (Deptt < = 10)
{
System.debug('Salesforce');
Deptt = Deptt + 1;
}
Output
Salesforce Salesforce Salesforce Salesforce Salesforce Salesforce Salesforce Salesforce Salesforce Salesforce
5. Example of a For Loop
for(Integer J=0; J<=5; J++)
{
System.debug('Webkul SalesForce Department');
}
Output
Webkul SalesForce Department Webkul SalesForce Department Webkul SalesForce Department Webkul SalesForce Department Webkul SalesForce Department Webkul SalesForce Department
6. Example of an array
integer[] a = new integer[4]; a[0] = 10; a[1] = 20; System.debug(a[0]);
Output
10
7. Assign 10 elements and print in reverse order
integer[] a = new integer[10];
for(integer i=0; i<10; i++)
a[i] = i * 10;
for(integer i=0; i<10; i++)
System.debug(a[i]);
System.debug('Array Elements Reverse Order');
for(integer i = a.size()-1; i>=0; i--)
System.debug(a[i]);
Output
10
20
30
40
50
60
70
80
90
Array Elements Reverse Order
90
80
70
60
50
40
30
20
10
8. String Array Example
integer[] a = new integer[] {10, 20, 30};
string [] s = new string[] {'salesforce', 'Visualforse', 'cloud computing'};
for(integer i=0; i<a.size(); i++)
System.debug( a[i] +' ' + s[i] );
Output
10 salesforce 20 Visualforse 30 cloud computing
9. To find the biggest element in the array
integer [] a = new integer[] {100, -20, 30};
for(integer i=0; i<a.size()-1; i++){
if( a[i] > a[i+1]){
integer temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
System.debug('The biggest elememnt is - ' + a[a.size()-1]);
Output
The biggest elememnt is - 100
What are Governor Limits and why it’s important?
Governor limits exist to control how much data you can share over the Salesforce database.
Apex is based on a multitenant architecture, where resources are shared by multiple clients/customers.
The governor’s limits ensure that no single customer monopolizes the resources.
And, they’re the reason your code will fail in production if you don’t respect them.
You get these limits per transaction:
- 100 SOQL queries – Use them wisely
- 150 DML statements – Batch your updates
- 50,000 total records retrieved – Be selective in queries
- 6 MB heap size – Don’t load massive data sets into memory
- 10 minutes for each Apex execution– For synchronous transactions
Where to Go From Here?
You can understand Salesforce Apex basics in more detail in this developer guide.
When you get to know the synchronous and asynchronous (Batch Apex) execution, then you can write tests that actually test things.
Here are some of the practices you should follow as the next steps:
- Build something real. Relying only on the theory part will never get you far that you can develop critical apps. So pick a business problem and solve it with Apex.
- Read other people’s code. Check out AppExchange packages, look at Salesforce’s sample code, and find open-source Salesforce projects on GitHub.
- Learn trigger frameworks. We touched on the basics, but enterprise Salesforce implementations use sophisticated frameworks.
- Master integration. Most Salesforce orgs don’t exist in isolation. Learn how to work with REST APIs, handle authentication, and deal with external systems.
If you’re looking for professional help with Salesforce development, Webkul’s team has worked on implementations across industries.
Sometimes it’s faster to bring in experts than to figure everything out yourself.
Support
If you have any queries related to Salesforce Apex development, reach out to us at [email protected].

Be the first to comment.