Salesforce - Developer - Apex
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro.htm
https://developer.salesforce.com/page/Apex
// Salesforce - Developer - Apex:
Apex has syntax and concepts that are similar to Java and C# programming
language. It has the usual array of features such as classes, interfaces,
constants, class variables, and annotations.
Interface is just an empty specification. It is a contractual list of methods
that other classes will need to implement if these other classes need to
implement this interface.
public interface PurchaseOrder {
Double discount();
}
public class CustomPurchaseOrder implements PurchaseOrder {
static final double DEFAULT_DISCOUNT = 0.05;
public Double discount() {
return DEFAULT_DISCOUNT;
}
}
The implement keyword is used to indicate interface implementation. The static
keyword indicates a class variable or method. We can access class variables
and methods using the class name, without having to create an instance of the
class:
CustomPurchaseOrder.DEFAULT_DISCOUNT;
CustomPurchaseOrder.discount();
The final keyword indicates a constant.
We can create an instance of a class using the new keyword:
PurchaseOrder po = new CustomPurchaseOrder();
Apex supports the standard Java-like notions of this and instanceOf.
Apex is a strongly-typed object-oriented language that allows developers to
execute flow and transaction control statements on the Force.com platform.
Apex enables developers to add business logic to most system events, including
button clicks, related record updates, and Visualforce pages.
Anonymous Code Block
Apex Triggers
Visualforce Classes
Apex Email Service
Asynchronous Apex
Future Methods
Queueable Apex
Schedulable Apex
Batch Apex
Data Types:
In Apex, all variables and expressions have a data type that is one of the
following:
1. Primitive
Boolean - can only be true, false, or null
Decimal - a number that includes decimal point
Double - a 64-bit number that includes decimal point
ID - Any Salesforce.com ID
Integer - a 32-bit number that does not include decimal point
Long - a 64-bit number that does not include decimal point
String - Any set of character surrounded by single quotes
Date - a value that indicates a particular day
DateTime - a value that indicates a particular day and time
Time - A value that indicates a particular time
2. sObject: any object that can be stored in the Force.com platform database.
An sObject variable represents a row of data.
3. Collection: List, Set, Map
String a = 'Hello';
Integer i = 20;
Decimal d = 20.56;
Double a = 30.45;
// sObject
Account a = new Account(Name = 'IBM', Type = 'Customer-Direct');
Account a = [select Name, Type, Industry from Account where Name = 'Dell'];
a.Name;
String industry = a.Industry;
if (industry == 'Technology') {
...
} else {
...
}
if (e == null) {
delete c;
}
A list is an ordered collection of Primitives or sObjects that are distinguished
by their indices. The index position of the first element in a list is always
0.
List<Integer> myList = new List<Integer>(); // Define a new list.
myList.add(47);
Integer i = myList.get(0);
myList.set(0, 1);
myList.clear();
List<String> colors = new List<String>('red','green','pink','purple');
List<String> is the same as String[]
List<Account> a = [select Name, type, industry from Account where Industry='Energy']
Notice the select statement that is used with a list, and the select statement
that is used with a sObject. These statements are the same. In sObject case,
it retrieve only one record. In the List case, it retrieve multiple records.
Variables
Methods: functions
Class: hold variables, and methods.
public class c1 {
private class c2 {
}
}
Account
Contact
Opportunity
Account a = new Account(Name = 'IBM', Type='Customer');
insert a;
Contact c = [select FirstName,LastName,Email from Contact where Email='...'];
Apex has List, Set, Map
List<Account> accts = [select Name,type from Account where Industry='Energy'];
List<Contact> contacts = [select Email from Contact where Email is NOT NULL];
Set<Integer> s = new Set<Integer>(); // Define a new set
s.add(1) // add an element to a set
System.assert(s.contains(1)); // Assert that the set contains an element
s.remove(1)
A map is a key-value pairs where each unique key maps to some value(s).
Map<Integer,String> m = new Map<Integer,String>(); // Define a new map
m.put(1,'First entry'); // insert a new key-value pair in the map
Map<String,String> currencies = new Map<String,String>(
'USA' => 'Dollars', 'India' => 'Rupees');
String US_currency = currencies.get('USA');
Apex Class:
public class c1 {
String hello = 'Hello, how are you?';
public String getHello() {
return hello;
}
}
public class Human {
private String name;
private String height;
private Integer weight;
public void walk() {
System.debug('Walking');
}
}
Human person = new Human();
public class Human {
public double work() {
Double check;
check = 1500.00;
return check;
}
}
Double paycheck;
Human person = new Human();
paycheck = person.work();
System.debug(paycheck);
Boolean (true, false, or null)
Integer, Long, Double, Decimal
Date, DateTime, Time
String
Enum
Blob
ID // Specific to Force.com and Apex
Account acc = new Account(Name = 'Acme');
Database.insert(acc);
List<Account> newAccounts = new List<Account>());
// Code to populate newAccounts
Database.insert(newAccounts);
Position__c pos = [SELECT Name FROM Position__c LIMIT 1];
The above code returns a single sObject
List<Position__c> positions = [SELECT Name FROM Position__c];
The above code returns a list of sObjects.
Integer x = [SELECT COUNT() FROM Position__c];
The above code returns an integer when an aggregation is specified.
List<Account> accLst = new List<Account>([SELECT ID, Name,
(SELECT Id, Amount, Probability FROM Opportunities) FROM
Account LIMIT 200]);
List<Contact> contacts = [SELECT Id,Name FROM Contact WHERE MailingState='tx'];
Map<Id,Contact> contactMap = new Map<Id,Contact>([SELECT Id, Name
FROM Contact WHERE MailingState='tx']);
Map<Id,Contact> contactMap = new Map<Id,Contact>(contacts);
// Query up through relationship fields:
[SELECT Id, Name, Account.Name, Account.MailingStreet, CustomObj__r.Name
FROM Contact
WHERE Account.Name like '%Banana%']
The total number of records that can be returned by SOQL queries in a request is
50,000. If returning a large set of queries causes you to exceed your heap limit,
then a SOQL query for loop must be used instead. It can process multiple batches
of records through the use of internal calls to query and queryMore. For
example, if the results are too large, the syntax below causes a runtime
exception:
Account[] accts = [SELECT id FROM account];
Instead, use a SOQL query for loop as in one of the following examples:
Account[] accts = new Account[];
for (List<Account> acct : [SELECT id, name FROM account
WHERE name LIKE 'Acme']) {
// Your logic here
accts.add(acct);
}
update accts;
Let the Force.com platform chunk your large query results into batches of 200
records by using this syntax where the SOQL query is in the for loop definition,
and then handle the individual datasets in the for loop logic.
Anonymous code block is Apex code that does not get stored in the metadata but
that can be compiled and executed using Developer Console. You can use anonymous
blocks to quickly evaluate Apex on the fly, or to write code that changes
dynamically at runtime.
page revision: 10, last edited: 16 Jan 2017 02:25