Salesforce - Developer - 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() {

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 


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 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

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 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 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'];
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

List<Integer> myList = new List<Integer>(); // Define a new list.
Integer i = myList.get(0);
myList.set(0, 1);
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.

Methods: functions
Class: hold variables, and methods.

public class c1 {
  private class c2 {


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


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() {
Human person = new Human();

public class Human {
  public double work() {
    Double check;
    check = 1500.00;
    return check;
Double paycheck;
Human person = new Human();
paycheck =;

Boolean (true, false, or null)
Integer, Long, Double, Decimal
Date, DateTime, Time
ID // Specific to and Apex

Account acc = new Account(Name = 'Acme');

List<Account> newAccounts = new List<Account>());
// Code to populate 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 

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
update accts;

Let the 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.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License