Grasping Core Concepts In Apex

Apex code generally includes various elements that may be recognizable to those familiar with other programming languages.

The section describes the basic functionality of Apex, as well as some of the core concepts.

Utilizing Version Configuration

In the Salesforce user interface you can specify a version of the Salesforce API against which to save your Apex class or trigger. This setting indicates not only the version of SOAP API to use, but which version of Apex as well. You can change the version after saving. Every class or trigger name must be unique. You cannot save the same class or trigger against different versions.

You can also use version settings to associate a class or trigger with a particular version of a managed package that is installed in your organization from AppExchange. This version of the managed package will continue to be used by the class or trigger if later versions of the managed package are installed, unless you manually update the version setting. To add an installed managed package to the settings list, select a package from the list of available packages. The list is only displayed if you have an installed managed package that is not already associated with the class or trigger.

For more information about using version settings with managed packages, see About Package Versions in the Salesforce online help.

Naming Variables, Methods, and Classes

You cannot use any of the Apex reserved keywords when naming variables, methods or classes. These include words that are part of Apex and the Lightning platform, such as listtest, or account, as well as reserved keywords.

Utilizing Variables and Expressions

Apex is a strongly-typed language, that is, you must declare the data type of a variable when you first refer to it. Apex data types include basic types such as Integer, Date, and Boolean, as well as more advanced types such as lists, maps, objects and sObjects.

Variables are declared with a name and a data type. You can assign a value to a variable when you declare it. You can also assign values later. Use the following syntax when declaring variables:

datatype​ variable_name [ = value];

Tip

Note that the semi-colon at the end of the above is not optional. You must end all statements with a semi-colon.

The following are examples of variable declarations:

// The following variable has the data type of Integer with the name Count, 
// and has the value of 0.
Integer Count = 0;
// The following variable has the data type of Decimal with the name Total. Note 
// that no value has been assigned to it.
Decimal Total;
// The following variable is an account, which is also referred to as an sObject.
Account MyAcct = new Account();

In Apex, primitive data type arguments like Integer or String are passed into methods by value. This implies that any modifications to these arguments are confined to the method’s scope, and once the method concludes, these alterations are discarded.

On the other hand, non-primitive data type arguments, such as sObjects, are passed into methods by reference. Consequently, after the method execution, the provided argument still points to the same object as it did before the method invocation. While the reference itself cannot be altered to point to a different object within the method, the values of the object’s fields can be modified.

Employing Statements


A statement is a coded instruction that carries out an action.

In Apex, statements are terminated with a semicolon and can fall into one of the following categories:

  • Assignment: Involves assigning a value to a variable.
  • Conditional (if-else)
  • Loops:
    • Do-while
    • While
    • For
  • Locking
  • Data Manipulation Language (DML)
  • Transaction Control
  • Method Invoking
  • Exception Handling

A block is a sequence of statements enclosed within curly braces and can be applied anywhere a single statement is acceptable. For instance:

if (true) {
    System.debug(1);
    System.debug(2);
} else {
    System.debug(3);
    System.debug(4);
}

In cases where a block consists of only one statement, the curly braces can be left off. For example:

if (true) 
    System.debug(1);
else 
    System.debug(2);
Leveraging Collections

Apex encompasses the following categories of collections:

  • Lists (arrays)
  • Maps
  • Sets

A list is a compilation of elements, including Integers, Strings, objects, or other collections. It is suitable when maintaining the sequence of elements is crucial, and duplicates are allowed.

The initial index position in a list is consistently 0.

To initiate a list:

  • Utilize the new keyword
  • Employ the List keyword, specifying the element type within <> brackets.

The creation of a list follows the syntax:

List <datatype> list_name
   [= new List<datatype>();] |
   [=new List<datatype>{value [, value2. . .]};] |
   ;

The following example creates a list of Integer, and assigns it to the variable My_List. Remember, because Apex is strongly typed, you must declare the data type of My_List as a list of Integer.

List<Integer> My_List = new List<Integer>();

For more information, see Lists.

set is a collection of unique, unordered elements. It can contain primitive data types, such as String, Integer, Date, and so on. It can also contain more complex data types, such as sObjects.

To create a set:

  • Use the new keyword
  • Use the Set keyword followed by the primitive data type contained within <> characters

Use the following syntax for creating a set:

Set<datatype> set_name 
   [= new Set<datatype>();] |
   [= new Set<datatype>{value [, value2. . .] };] |
   ;

The following example creates a set of String. The values for the set are passed in using the curly braces {}.

Set<String> My_String = new Set<String>{'a', 'b', 'c'};

For more information, see Sets.

map is a collection of key-value pairs. Keys can be any primitive data type. Values can include primitive data types, as well as objects and other collections. Use a map when finding something by key matters. You can have duplicate values in a map, but each key must be unique.

To create a map:

  • Use the new keyword
  • Use the Map keyword followed by a key-value pair, delimited by a comma and enclosed in <> characters.

Use the following syntax for creating a map:

Map<key_datatype, value_datatype> map_name
   [=new map<key_datatype, value_datatype>();] | 
   [=new map<key_datatype, value_datatype>
   {key1_value => value1_value 
   [, key2_value => value2_value. . .]};] |
   ;

The following example creates a map that has a data type of Integer for the key and String for the value. In this example, the values for the map are being passed in between the curly braces {} as the map is being created.

Map<Integer, String> My_Map = new Map<Integer, String>{1 => 'a', 2 => 'b', 3 => 'c'};

For more information, see Maps.

Implementing Branching

An if statement is a true-false test that enables your application to do different things based on a condition. The basic syntax is as follows:

if (Condition){
// Do this if the condition is true
} else {
// Do this if the condition is not true
}
Employing Loops

While the if statement enables your application to do things based on a condition, loops tell your application to do the same thing again and again based on a condition. Apex supports the following types of loops:

  • Do-while
  • While
  • For

Do-while loop checks the condition after the code has executed.

While loop checks the condition at the start, before the code executes.

For loop enables you to more finely control the condition used with the loop. In addition, Apex supports traditional For loops where you set the conditions, as well as For loops that use lists and SOQL queries as part of the condition.

For more information, see Loops.