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:
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:
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:
Collections
Apex has the following types of collections:
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:
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:
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.
Branching
If-else statement
Loops
Do-While
Loop repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
Ex:
While
Repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
Ex:
For
Apex supports three variations of the for loop:
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
Apex has the following types of collections:
- Lists (arrays)
- Maps
- Sets
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
<
String
> My_String =
new
Set
<
String
>{
'a'
,
'b'
,
'c'
};
MapA 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
<
Integer
,
String
> My_Map =
new
Map
<
Integer
,
String
>{
1
=>
'a'
,
2
=>
'b'
,
3
=>
'c'
};
Branching
If-else statement
1 | if (Condition){ |
2 | // Do this if the condition is true |
3 | } else { |
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:
1 | do { |
2 | code_block |
3 | } while (condition); |
Ex:
1 | Integer count = 1 ; |
2 |
3 | do { |
4 | System .debug(count); |
5 | count++; |
6 | } while (count < 11 ); |
While
Repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
1 | while (condition) { |
2 | code_block |
3 | } |
Ex:
1 | Integer count = 1 ; |
2 |
3 | while (count < 11 ) { |
4 | System .debug(count); |
5 | count++; |
6 | } |
For
Apex supports three variations of the for loop:
- The traditional for loop:
1
for
(init_stmt; exit_condition; increment_stmt) {
2
code_block
3
}
- The list or set iteration for loop:
1
for
(variable : list_or_set) {
2
code_block
3
}
- The SOQL for loop:
1
for
(variable : [soql_query]) {
2
code_block
3
}
1
for
(variable_list : [soql_query]) {
2
code_block
3
}
Comments
Post a Comment