Skip to content

Basic Apex Tutorial

Salesforce Apex

Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform. Apex is similar to Java and C#, and it enables developers to add business logic to most system events, such as button clicks, record updates, and Visualforce pages. In this tutorial, we will cover some basic concepts of the Apex language, including variables, data types, classes, methods, and triggers.


  1. Variables and Data Types

Apex supports several primitive data types, such as Integer, Double, Long, Date, Datetime, Time, String, ID, and Boolean. Let’s declare some variables:

Integer myInt = 5;
Double myDouble = 5.5;
String myString = 'Hello, Apex!';
Boolean myBool = true;
  1. Lists, Sets, and Maps

Apex provides three types of collections: Lists (ordered), Sets (unordered), and Maps (key-value pairs). Here’s how to declare and use them:

// Lists
List<String> stringList = new List<String>();

// Sets
Set<String> stringSet = new Set<String>();

// Maps
Map<String, String> stringMap = new Map<String, String>();
stringMap.put('A', 'Apple');
stringMap.put('B', 'Banana');
stringMap.put('C', 'Cherry');
  1. Classes and Objects

In Apex, you can define classes to create reusable code components. Here’s an example of a simple class with a method:

public class HelloWorld {
    public static String sayHello() {
        return 'Hello, World!';

To use this class, you can call the sayHello method like this:

String greeting = HelloWorld.sayHello();
  1. Triggers

Triggers are a special type of Apex code that is executed whenever a specific event occurs, such as when a record is created, updated, or deleted. Here’s an example of a trigger that updates a custom field when an Account record is created:

trigger UpdateAccountRating on Account (before insert) {
    for (Account acc : {
        acc.Rating__c = 'New Account';

This trigger runs before the Account record is inserted, and it sets the custom Rating__c field to “New Account”.

  1. SOQL (Salesforce Object Query Language)

SOQL is used to query Salesforce data. It is similar to SQL but specific to Salesforce. Here’s an example of querying Account records:

List<Account> accounts = [SELECT Id, Name, Rating__c FROM Account WHERE Rating__c = 'New Account' LIMIT 10];
for (Account acc : accounts) {
    System.debug('Account Name: ' + acc.Name);

In this example, we retrieve the first 10 Account records with a Rating__c value of “New Account” and output their names to the debug log.

These are just the basics of the Apex language. Apex provides many more features, such as exception handling, test classes, and integration with external services. As you learn more about Apex, you’ll be able to develop complex applications that harness the full power of the Salesforce platform.


Common Apex String Methods

Working with Time in Salesforce Apex

Salesforce Apex security best practices

Speeding Up Apex Test Execution: A Guide for Faster Salesforce Deployments

Join the conversation

Your email address will not be published. Required fields are marked *

error: Content is protected !!