APEX Short Notes - Introduction

Hi All,

This is my new blog on Saleforce development. I have started my new job as a Saleforce Developer and I found it very interesting. So, I went through Saleforce developer documentations which you can find by going here.
And I know, it is very detailed and lengthy documentation. So I thought of cut down it and create a short notes of it which I think is important.
So lets get started with learning APEX; the programming language for Saleforce development.

APEX looks like Java and acts like database stored procedures
As a language, Apex is: Integrated
Apex provides built-in support for common Lightning Platform idioms, including:

  • Data manipulation language (DML) calls, such as INSERT, UPDATE, and DELETE, that include built-in DmlExceptionhandling
  • Inline Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) queries that return lists of sObject records
  • Looping that allows for bulk processing of multiple records at a time
  • Locking syntax that prevents record update conflicts
  • Custom public API calls that can be built from stored Apex methods
  • Warnings and errors issued when a user tries to edit or delete a custom object or field that is referenced by Apex

Apex provides built-in support for unit test creation and execution.

You can save your Apex code against different versions of the API. This enables you to maintain behavior.

Using Variables and Expressions

Apex data types include basic types such as Integer, Date, and Boolean, as well as more advanced types such as lists, maps, objects and sObjects.

Use the following syntax when declaring variables:
datatype variable_name [ = value];

Note: Semi-colon at the end of the above is a must. You must end all statements with a semi-colon.
Ex:
Integer Count = 0;
Decimal Total;
Account MyAcct = new Account();

Pass by value 
In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value.
This fact means that any changes to the arguments exist only within the scope of the method. 

Pass by reference 
Non-primitive data type arguments, such as sObjects, are passed into methods by reference.
Therefore, when the method returns, the passed-in argument still references the same object as before the method call.

Statements
A statement is any coded instruction that performs an action.
In Apex, statements must end with 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 (DML) 
  • Transaction Control 
  • Method Invoking 
  • Exception Handling

Collections 
Apex has the following types of collections:
  • Lists (arrays) 
  • Maps 
  • Sets
List 
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.
Ex: List<Integer> My_List = new List<Integer>();

Set 
A set is a collection of unique, unordered elements. 
It can contain primitive data types, such as String, Integer, Date, and so on. 
It can also contain more complex data types, such as sObjects.
Ex: Set<String> My_String = new Set<String>{'a''b''c'};

Map
A map is a collection of key-value pairs. Keys can be any primitive data type. 
Values can include primitive data types, as well as objects and other collections. 
Use a map when finding something by key matters. 
You can have duplicate values in a map, but each key must be unique. 
The following example creates a map that has a data type of Integer for the key and String for the value. 
Map<IntegerString> My_Map = new Map<IntegerString>{1 => 'a'2 => 'b'3 => 'c'};


Branching
If-else statement

1if (Condition){
2// Do this if the condition is true
3else {
4// Do this if the condition is not true
5}

Loops

Do-While 
Loop repeatedly executes a block of code as long as a particular Boolean condition remains true.

Syntax:
1do {
2   code_block
3while (condition);

Ex:
1Integer count = 1;
2
3do {
4    System.debug(count);
5    count++;
6while (count < 11);

While
Repeatedly executes a block of code as long as a particular Boolean condition remains true.

Syntax:
1while (condition) {
2    code_block
3}

Ex:
1Integer count = 1;
2
3while (count < 11) {
4    System.debug(count);
5    count++;
6}

For
Apex supports three variations of the for loop:
  • The traditional for loop:
    1for (init_stmt; exit_condition; increment_stmt) {
    2    code_block
    3}
  • The list or set iteration for loop:
    1for (variable : list_or_set) {
    2    code_block
    3}
    where variable must be of the same primitive or sObject type as list_or_set.
  • The SOQL for loop:
    1for (variable : [soql_query]) {
    2    code_block
    3}
    or
    1for (variable_list : [soql_query]) {
    2    code_block
    3}
Both variable and variable_list must be of the same sObject type as is returned by the soql_query.



Comments