Show Menu
Cheatography

Apex basic Cheat Sheet (DRAFT) by

Apex is a proprietary language developed by Salesforce.com. It is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Apex

What is Apex
Apex is a strongly typed, object­­-o­r­i­ented Progra­­mming language that allows developers to execute the flow and transa­­ction control statements on the force.com platform server in conjun­­ction with calls to the Froce.com API.

Unders­­ta­nding the Apex Syntax

Variable Decalation
Loop Statement
Flow Control Statement
List Datatype
Map Datatype
Set Datatype
DML Statement
SOQL Query
Apex Trigger
 

Data types

Boolean
Boolean isTrue = true;
String
String greeting = 'Hello, Salesf­orce!';
Integer
Integer quantity = 10;
Long
Long bigNumber = 123456­7890;
Decimal
Decimal price = 99.99;
Double
Double largeValue = 123456­789­0.1­234­56789;
Date
Date today = Date.t­oday();
Time
Time curren­tTime = Time.n­ow();
Datetime
Datetime curren­tDa­tetime = Dateti­me.n­ow();
ID
Id recordId = '001R0­000­012­345­6789';
Blob
Blob type is more complex and typically used for binary data like images.

List Datatype

Method List Datatype
add(li­stE­lement) or add(index, listEl­ement)
Adds the specified element to the end of the list.
contai­ns(­lis­tEl­ement)
Returns true if the list contains the specified element.
equals­(list2)
Compares the current list to another list, returning true if they are equal.
isEmpty()
Returns true if the list is empty.
remove­(index)
Removes the element at the specified position in the list.
size()
Returns the number of elements in the list.
sort()
Sorts the elements of the list in ascending order.
deepCl­one­(pr­ese­rveId, preser­veR­ead­onl­yTi­mes­tamps, preser­veA­uto­number)
Creates a deep clone of the list. You can specify whether to preserve record IDs, readonly timest­amps, and autonumber fields.

Set Datatypes

add(se­tEl­ement)
Adds an element to the set if it is not already present.
clear()
Removes all of the elements from the set.
clone()
Makes a duplicate copy of the set.
contai­ns(­set­Ele­ment)
Returns true if the set contains the specified element.
equals­(set2)
Compares this set with the specified set and returns true if both sets are equal; otherwise, returns false.
isEmpty()
Returns true if the set has zero elements.
remove­(se­tEl­ement)
Removes the specified element from the set if it is present.
size()
Returns the number of elements in the set (its cardin­ality).
toString()
Returns the string repres­ent­ation of the set.

Map Datatype

clear()
Removes all of the key-value mappings from the map.
clone()
Makes a duplicate copy of the map.
contai­nsK­ey(key)
Returns true if the map contains a mapping for the specified key.
deepCl­one()
Makes a duplicate copy of a map, including sObject records if this is a map with sObject record values.
equals­(map2)
Compares this map with the specified map and returns true if both maps are equal; otherwise, returns false.
get(key)
Returns the value to which the specified key is mapped, or null if the map contains no value for this key.
isEmpty()
Returns true if the map has zero key-value pairs.
keySet()
Returns a set that contains all of the keys in the map.
put(key, value)
Associates the specified value with the specified key in the map.
remove­(key)
Removes the mapping for the specified key from the map, if present, and returns the corres­ponding value.
size()
Returns the number of key-value pairs in the map.
toString()
Returns the string repres­ent­ation of the map.
 

Loop Statement

For Each Loop:
List<S­tri­ng> names = new List<S­tri­ng>­{'A­lice', 'Bob', 'Charl­ie'};
for (String name : names) {
System.de­bug­(name);
}

Tradit­ional For Loop:
for (Integer i = 0; i < 5; i++) {
System.de­bug(i);
}

Triggers - Context Variable

Trigge­r.new
Contains a list of newly created or updated records.
Trigge­r.old
Contains a list of old records before they were updated.
Trigge­r.n­ewMap
Contains a mapping between IDs and objects for all new records.
Trigge­r.o­ldMap
Contains a mapping between IDs and objects for all old records.
Trigge­r.i­sInsert
Returns true if the trigger is running due to an Insert operation.
Trigge­r.i­sUpdate
Returns true if the trigger is running due to an Update operation.
Trigge­r.i­sDelete
Returns true if the trigger is running due to a Delete operation.
Trigge­r.i­sBefore
Returns true if the trigger is running before data is written to the database.
Trigge­r.i­sAfter
Returns true if the trigger is running after data is written to the database.
Trigge­r.i­sUn­delete
Returns true if the trigger is running due to an Undelete operation.
 

SOQL

SELECT Fields FROM Object
SELECT Field1, Field2 FROM ObjectName WHERE Condition
ORDER BY Clause
SELECT Name, Create­dDate FROM Account ORDER BY Create­dDate DESC
LIMIT Clause
SELECT Name FROM Account LIMIT 10
GROUP BY Clause
SELECT Industry, COUNT(Id) FROM Account GROUP BY Industry
Aggregate Functions
SELECT AVG(Am­ount) FROM Opport­unity
Relati­onship Queries
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
Date Functions
SELECT Name FROM Account WHERE Create­dDate = THIS_MONTH

Aggregate Functions

COUNT()
Counts the number of records in the query result.
SUM()
Calculates the sum of a numerical field.
AVG()
Calculates the average value of a numerical field.
MIN()
Finds the minimum value of a numerical field.
MAX()
Finds the maximum value of a numerical field.

Date and time functions

TODAY
Returns the current date (excluding hours, minutes, seconds).
YESTERDAY
Returns the date of yesterday.
THIS_MONTH
Returns all records created in the current month.
LAST_N­_DAYS:n
Returns all records created in the last n days.
NEXT_N­_DAYS:n
Returns all records that will be created in the next n days.
CALEND­AR_­MON­TH(­fie­ldName)
Returns the month of a specific date field.
DAY_ON­LY(­fie­ldName)
Returns the day of a specific date field
THIS_YEAR
Returns all records created in the current year.