Apex code typically contains many things that you might be familiar with from other programming languages:
The section describes the basic functionality of Apex, as well as some of the core concepts.
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.
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 Force.com platform, such as list, test, or account, as well as reserved keywords.
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];
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, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.
Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call and can't be changed to point to another object. However, the values of the object's fields can be changed in the method.
A statement is any coded instruction that performs an action.
A block is a series of statements that are grouped together with curly braces and can be used in any place where a single statement would be allowed. For example:
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);
Apex has the following types of collections:
A list is a collection of elements, such as Integers, Strings, objects, or other collections. Use a list when the sequence of elements is important. You can have duplicate elements in a list.
The first index position in a list is always 0.
To create a list:
Use the following syntax for creating a list:
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.
A 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 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.
A 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 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.
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 }
For more information, see Conditional (If-Else) Statements.
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:
A Do-while loop checks the condition after the code has executed.
A While loop checks the condition at the start, before the code executes.
A 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.