Apex
Apex triggers, classes, web services,...
Install / Use
/learn @SalesforceSFDC/ApexREADME
Apex
<img src="https://login.salesforce.com/img/logo190.png" align="right" width="300">
- PMD Source Code Analyzer Project
- Apex Reserved Keywords
- Object Reference for Salesforce and Force.com
- Starting With Apex
- The
globalaccess modifier, which is more permissive than thepublicmodifier and allows access across namespaces and applications. - http://avionsalesforce.blogspot.com/
- https://trendspro.builtwith.com/dashboard
- https://success.salesforce.com/ideaView?id=08730000000DfGeAAK
- https://medium.com/airbnb-engineering/airflow-a-workflow-management-platform-46318b977fd8
- https://salesforce.stackexchange.com/questions/23338/trigger-to-update-parent-object-value-with-child-value
Table of Contents
- Sections
- How Does Apex Work?
- Apex Core Concepts
- Execution Context
- List
- Maps
- Trigger and Bulk Request Best Practices
- Using Maps and Sets in Bulk Triggers
- Loops
- Test Driven Development Process
- Classes, Objects and Interfaces
- Integrations
- Static and Instance Methods, Variables, and Initialization Code
- Triggers
- Apex Test Classes
- Execution Governor and Limits
- Data Maninpulation Language
- Apex Class Metadata Templates
- Manipulate records with DML
- Getter Setter
- Class Methods
- Batch
- Einstein
- SOAP
How Does Apex Work?
When a developer writes and saves Apex code to the platform, the platform application server first compiles the code into an abstract set of instructions that can be understood by the Apex runtime interpreter, and then saves those instructions as metadata.
When an end-user triggers the execution Apex, perhaps by clicking a button or accessing a Visualforce page, the platform application server retrieves the compiled instructions from the metadata and sends them through the runtime interpreter before returning results.
All Apex code runs on the Force.com platform. To guarantee consistent performance and scalability, the execution of Apex is bound by governor limits that ensure no single Apex execution impacts the overall service of Salesforce. This means all Apex code is limited by number of operations (such as DML or SOQL) that it can perfrom within one process. All Apex requests return a collection that contains from 1 to 50,000 records. You cannot assume that your code works only on a single records at a time. Therefore, you must implement programming patterns that take bulk processing into account. Otherwise, you may run into governor limits.
public method =
private helper method - it cannot be called externally because it is private.
member variables = attributes
accessor methods = used to access attributes
instance method =
Static method = use static keyword
Static methods are easier to call than instance methods because they dont need to vbe called on an instance of the class but are called directly on the class name.
public scope =
public static method
Methods are defined in a class and objects are instances of a given class.
Constructor is a method that is called at the beginning of an object's lifetime to create and intialize the object.
A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
Instance variables (non-static fields) are unique to each instance of a class. Class variables (static fields) are fields declared with the static modifier; there is exactly one copy of a class variable, regardless of how many times the class has been instantiated. Local variables store temporary state inside a method. Parameters are variables that provide extra information to a method; both local variables and parameters are always classified as "variables" (not "fields").
- The term "instance variable" is another name for non-static field.
- The term "class variable" is another name for static field.
- A local variable stores temporary state; it is declared inside a method.
- A variable declared within the opening and closing parenthesis of a method is called a parameter.
- What are the eight primitive data types supported by the Java programming language? byte, short, int, long, float, double, boolean, char
- Character strings are represented by the class java.lang.String.
- An array is a container object that holds a fixed number of values of a single type.
All objects have state and behavior, that is, things that an object knows about itself, and things that an object can do.
Apex Core Concepts
Execution Context
An execution context has two characteristics:
- It defines the scope and lifetime of static variables.
- It defines the context for those governor limits that are preset between execution contexts.
Static Variables
There lifetime and scope is defined by the execution context. In other words, static variables can only be accessed from within the execution context in which they are created, and are deleted when the execution context completes.
- Static variables do not persist information between execution contexts. They cannot be used to keep track of the overall execution of the app, or to cache data or objects for use while the app is running.
- Apex does not support the equivalent of application or session variables. Anything you wish to persist must be stored in database objects or custom settings.
- There is no need for synchronization. A given execution context runs on a single thread, so static variables are, in effect, the equivalent of thread local storage - each thread has its own copy of these variables and there is no need to synchronize access.
Variables
Use the following syntax when declaring variables:
datatype variable_name [ = value];
Using Statements
In Apex, statements must end in a semicolon and can be one of the following types:
- Assignment, such as assigning a value to a variable.
- Conditional (if-else)
- Loops:
- Do-while
- While
- For
- Locking
- Data Manipulation Language
- Transaction control
- Method invoking
- Exception handling
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.
if (true) {
System.debug(1);
System.debug(2);
} else {
System.debug(3);
System.debug(4);
}
Collections
Apex has the following types of collections:
- Lists (Arrays)
- Maps
- Sets
Lists
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
newkeyword - Use the
Listkeyword followed by the element type contained within <> characters.
To create 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. 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>();
A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Lists can contain any collection and can be nested within one another and become multidimensional. For example, you can have a list of lists of sets of Integers. A list can contain up to four levels of nested collections inside it, that is, a total of five levels overall.
To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within <> characters.
For example:
// Create an empty list of String
List<String> my_list = new List<String>();
// Create a nested list
List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();
To access elements in a list, use the List methods provided by Apex. For example:
List<Integer> myList = new List<Integer>(); // Define a new list
myList.add(47); // Adds a second element of value 47 to the end
// of the list
Integer i = myList.get(0); // Retrieves the element at index 0
myList.set(0, 1); // Adds the integer 1 to the list at index 0
myList.clear(); // Removes all elements from the list
Sets
A
