Apex
What is Apex
Apex is a strongly typed, object-oriented Programming language that allows developers to execute the flow and transaction control statements on the force.com platform server in conjunction with calls to the Froce.com API. |
Understanding 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, Salesforce!'; |
Integer |
Integer quantity = 10; |
Long |
Long bigNumber = 1234567890; |
Decimal |
Decimal price = 99.99; |
Double |
Double largeValue = 1234567890.123456789; |
Date |
Date today = Date.today(); |
Time |
Time currentTime = Time.now(); |
Datetime |
Datetime currentDatetime = Datetime.now(); |
ID |
Id recordId = '001R00000123456789'; |
Blob |
Blob type is more complex and typically used for binary data like images. |
List Datatype
Method List Datatype |
add(listElement) or add(index, listElement) |
Adds the specified element to the end of the list. |
contains(listElement) |
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. |
deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber) |
Creates a deep clone of the list. You can specify whether to preserve record IDs, readonly timestamps, and autonumber fields. |
Set Datatypes
add(setElement) |
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. |
contains(setElement) |
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(setElement) |
Removes the specified element from the set if it is present. |
size() |
Returns the number of elements in the set (its cardinality). |
toString() |
Returns the string representation of the set. |
Map Datatype
clear() |
Removes all of the key-value mappings from the map. |
clone() |
Makes a duplicate copy of the map. |
containsKey(key) |
Returns true if the map contains a mapping for the specified key. |
deepClone() |
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 corresponding value. |
size() |
Returns the number of key-value pairs in the map. |
toString() |
Returns the string representation of the map. |
|
|
Loop Statement
For Each Loop:
List<String> names = new List<String>{'Alice', 'Bob', 'Charlie'};
for (String name : names) {
System.debug(name);
}
Traditional For Loop:
for (Integer i = 0; i < 5; i++) {
System.debug(i);
} |
Triggers - Context Variable
Trigger.new |
Contains a list of newly created or updated records. |
Trigger.old |
Contains a list of old records before they were updated. |
Trigger.newMap |
Contains a mapping between IDs and objects for all new records. |
Trigger.oldMap |
Contains a mapping between IDs and objects for all old records. |
Trigger.isInsert |
Returns true if the trigger is running due to an Insert operation. |
Trigger.isUpdate |
Returns true if the trigger is running due to an Update operation. |
Trigger.isDelete |
Returns true if the trigger is running due to a Delete operation. |
Trigger.isBefore |
Returns true if the trigger is running before data is written to the database. |
Trigger.isAfter |
Returns true if the trigger is running after data is written to the database. |
Trigger.isUndelete |
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, CreatedDate FROM Account ORDER BY CreatedDate 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(Amount) FROM Opportunity |
Relationship Queries |
SELECT Name, (SELECT LastName FROM Contacts) FROM Account |
Date Functions |
SELECT Name FROM Account WHERE CreatedDate = 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. |
CALENDAR_MONTH(fieldName) |
Returns the month of a specific date field. |
DAY_ONLY(fieldName) |
Returns the day of a specific date field |
THIS_YEAR |
Returns all records created in the current year. |
|