Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Learn About Apex Features and Terminology

Learning Objectives

After completing this unit, you’ll be able to:

  • Identify the core characteristics of the Apex coding language.
  • Describe key terminology and data types.

Apex Features

Apex was designed for easy use by developers. For this reason, it has several key features that make it a good fit when building business logic into systems. Specifically, Apex is:

Object-Oriented

Apex’s syntax mirrors popular programming languages like Java, making it easy to use for experienced coders. For example, variable declaration, loop syntax and conditional statements all work the same in Apex as they do in Java.

Based in Data

Apex is data-focused and designed to execute multiple queries and DML (Data Manipulation Language) statements at the same time.

Strongly Typed

Apex is a strongly typed language. It uses direct reference to objects, like sObject, and any invalid reference quickly fails if it is deleted or uses the wrong data type. Unlike some other languages, Apex is also generally case-insensitive.

Run in a Multitenant Environment

Apex runs in a multitenant environment, and its runtime engine is designed to avoid problems like runaway code – where a query takes too long to resolve. A combination of easy-to-understand error messages and automatic failure for code that violates resource limits allows you to keep mistakes from monopolizing your processing power. When code would normally cause a problem because of an error or inefficiency related to resources, Apex helps you find and fix the issue before it’s running rampant.

Built with Integrated Support

DML operations like INSERT, UPDATE, DELETE and also DML Exception handling all have built-in support with Apex. It also supports inline SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) query handling.

Automatically Upgraded

Apex’s functionality is upgraded alongside Salesforce’s capabilities, with no need to update Apex itself separately. However, each data Class has an API version that isn’t updated each release, which prevents code from breaking every time there’s a new version of Salesforce.

Easily Testable

Built-in support for unit test creation and execution helps you refine your projects, alongside test results that tell you how much code is covered. With easy access to quick, information-rich testing, it’s easier to feel confident in the code you create with Apex.

Data Types in Apex

To write code with Apex, you use a number of different data types. These include:

Primitives

Primitives are some of the simplest and most common data types in code. They include Integers, Long, Date, Datetime, String, ID, and Boolean values. Learn more about Primative Data Typeshere.

Collections

Made up of Lists, Sets, and Maps, collections can store multiple records.

sObject

A database table that stores specific information about a business, like accounts, contacts, or leads.

Enums

An abstract data type that defines a set of named constant values which don't have a numerical order.

Classes, Objects and Interfaces

As in Java, you can create classes in Apex. A class is a template or blueprint from which objects are created. An object is an instance of a class. An interface is like a class in which none of the methods have been implemented—the method signatures are there, but the body of each method is empty.

Interfaces help you separate the implementation of a method (or a specific block of code) from its declaration, which lets you create different implementations of a method for different applications.

These different data types are the bedrock of coding in Apex, and understanding how to use them is what allows you to build applications for your own use cases.

Apex Logic

Apex is designed to be familiar. If you’ve worked with other coding languages, you’ll probably see similarities in the syntax you’ll use with Apex. But if not, don’t worry! Once you understand the basic types of statements Apex coders use, you can apply them to accomplish your goals. Some important parts of that syntax to keep in mind are:

Loop Statement

A loop statement is used for iterating over a list or iterating over a piece of code for a specified number of times. It allows you to run code until a condition is met. Loops use logic like for and while to run specific code in response to specific conditions set by the programmer. For example, here’s a looping statement that prints the integers 1 through 10 to the debug log:

Integer count = 1;


do {
    System.debug(count);
    count++;
} while (count < 11);

Flow Control Statement

Apex uses the if or switch statements for flow control. This allows you to stop or start executing code based on a condition. You could run code if a record is updated, for example. Here’s an example of a control statement that differentiates between medals based on the placement of an individual.

if (place == 1) {
    medal_color = 'gold';
} else if (place == 2) {
    medal_color = 'silver';
} else if (place == 3) {
    medal_color = 'bronze';
} else {
    medal_color = null;
}

DML Statements and SQL Queries

Queries fetch the data from Salesforce database. This usually means looking for data from specific fields or objects, such as the Account object.

DML—Data Manipulation Language—statements allow you to update, delete, or otherwise alter records.

Put together, these statements let you take full advantage of the data stored in your iteration of Salesforce and perform operations that keep your data up to date with ease. For example, here’s a code block that inserts an account record and associated contact record:


Account accObject = new Account();
accObject.Name = 'Test Account’;
accObject.Website =’www.salesforce.com’;
Insert accObject ;
List<Contact> contactList = new List<Contact>();
Contact con1 = new Contact();
con1.FirstName =’first’;
con1.LastName =’contact’;
con1.AccountId = accObject.Id;
contactList.add(con1);
Insert contactList;

You learn how to use some of these features in actual code in the next section, but for now, it should be clear that Apex shares many of its syntax functions with familiar languages like Java. Once you have these basics of the language’s logic down, you’re on the road to writing functional code and expanding what Salesforce products can do for you.

Resources

Salesforce 도움말에서 Trailhead 피드백을 공유하세요.

Trailhead에 관한 여러분의 의견에 귀 기울이겠습니다. 이제 Salesforce 도움말 사이트에서 언제든지 새로운 피드백 양식을 작성할 수 있습니다.

자세히 알아보기 의견 공유하기