Apex is an object-oriented, strongly typed programming language, API supported by Force.com to give transaction control on salesforce platform. Apex supports for online SOQL and SOSL query handling and returns SObjects records. It has built-in support for DML and DML exception handling. It is easy to understand and use as the syntax is more similar to Java language.
Apex is used to implement complex business functionality. Apex supports xTriggerx. We can perform complex validation using Apex.
Note: SObject class is a generic class that can be any SFDC object.
Introduction of Collection Types, Loops x DML Statements
As per other programming languages, Apex also has some inbuilt data structures. Collectively it is called Collection. Apex Collection has four types namely Lists, Sets, Maps, and Enumerations. The first element of any collection type is always indexed at location zero.
List:
A list is a sequential collection of elements. It allows duplicates. Letâs consider the following example to  create List of integer
ListxIntegerx customList = new ListxIntegerx(); customList.add(55); customList.add(56); customList.get(0);
You can also use the array syntax for lists. For example:
String[] favoriteColor = new ListxStringx(); favoriteColor[2] = âBlueâ;
Set :
A set is another collection type. Unlike List, the set is unordered as well as doesnât allow duplicates.
SetxStringx alpha = new SetxStringx{âpâ,âqâ,ârâ}; alpha.add(âsâ); System.assert(alpha.comtains(ârâ));
Maps:
Maps are collections of types that support key-value pairs.
MapxString,Stringx myStrings = new MapxString,Stringx{âAS201â, âXYZâ};
Enumeration:
Consider the following example to create enumerated data types in Apex
public enum color {RED,BLACK,BLUE,ORANGE} color r = color.BLUE;
Loops :
Apex supports five types of procedural loops. Following are the listed loops. All loops allow break and continue statements.
- do{statement} while (condition);
- while(condition)Â statement;
- for(initialization;Â exit condition;Â increment)Â statement;
- for(variable : array/set) statement;
- for(variable : [inline_soql_query]) statement;
Do-While Loops
Similar to other programming languages â do-while loop repeatedly executes a block of code till the Boolean condition remains true. Its syntax is:
Do { --code }while(condition);
While Loops
The Apexââ while loop executes a block of code until a particular Boolean condition remains true. Its syntax is:
While(condition) --code }
For Loop:
Apex supports three variations of the for loop:
- The traditional for loop:
For(init statement; exit condition; increment/decrement statement){ Code block }
- The list or set iteration for loop:
For(variable : list_or_set) { code block;Â }
- The SOQLÂ for loop:
for (variable/variable list : [soql query statement]) { Code block; }
Here both variable and variable_list must be of the same sObject type.
Implementation of Apex Trigger
Apex triggers work like stored procedures execute when a particular event occurs. One can write pre-event and post-event trigger. e.g.
trigger xtrigger namex on ObjectName (trigger events) { --code here --code here }
Triggers are executed using events like insert, update, delete, upsert, merge, undelete etc.
Example 1
Suppose we received a business requirement that we need to create a Payroll Record when Employeexs xPresent Statusx field changes to present from absent. For this, we will create a trigger on APEX_Employee_c object by the following steps:
Step 1:Â Go to sObject
Step 2:Â Click on Employee
Step 3: Click on xNewx button in Trigger related list and add the trigger code as given below.
Trigger Employee_After_Insert on APEX_Employee_c (after update) { List PayrollList = new List(); for(APEX_Employee_c objEmployee: Trigger, new) { If(objemployee.APEX_Employee_Status_c == âPresentâ) { APEX_Payroll_c objInvoice = new APEX_Payroll_c(); objInvoice.APEX_status_c = âSalariedâ; PayrollList.add(objPayroll); } } }
Understanding x Use of Trigger Context Variables
All triggers use context variables which allow to access data at run time. These variables can be accessed thru Trigger class. Eg. Trigger, old, Trigger.isExecuting, Trigger.oldMap etc.
Context
Variable |
Usage |
isExecuting | It returns true if the current context of the Apex code is a trigger |
isInsert | It will return true if it is fired by insert operation |
isUpdate | It will return true if it is fired by update operation |
isDelete | It will return true if it is fired by delete operation |
isBefore | It will return true if it is fired by before operation |
isAfter | It will return true if it is fired by afterall operation |
isUndelete | It will return true if date is recovered from recycle bin |
new | It returns a list of the new versions of the sObject records. |
newMap | It returns a map of IDs for  sObject records. |
old | Returns a list of the old versions of the sObject records. |
oldMap | A map of IDs to the old map of the sObject records. |
size | The total number of records in a trigger invocation, both old and new. |
 Thatxs all for a brief introduction to Apex. If you need to refer old tutorials please feel free to read them using below links
Tutorial Index
- Introduction to Cloud Computing (Salesforce.com and Force.com)
- Overview of Database Concepts (Salesforce.com)
- Introduction to Force.com
- Building Salesforce Custom App and Objects
- Object Relationships and Formula Field in Salesforce
- Salesforce Security Model and Overview
- Automation in Salesforce
- Approval Process in Salesforce
- Introduction to SOQL and SOSL
- Introduction to Apex
- Salesforce Data Management
- Visualforce MVC Architecture on Cloud
- Salesforce Reports and Dashboards
- Building a Visualforce (Custom) Page for the Salesforce App
- Salesforce Sandbox and Overview of Force.com capabilities
- Learning Apex and Deployment Tools
x