Salesforce Tutorial# 10: Introduction to Apex

Apex is an object oriented, strongly typed programming language, used API supported by 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 trigger. We can perform complex validation using Apex.

Note: SObject class is generic class which can be any SFDC object.

Introduction of Collection Types, Loops & DML Statements

As per other programming languages Apex also have some inbuilt data structures. Collectively it is called as 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.


A list is a sequential collection of elements. It allows duplicates. Let’s consider the following example to  create List of integer

You can also use the array syntax for lists. For example:

Set :

A set is another collection type. Unlike List set is unordered as well as doesn’t allow duplicates.


Maps are collections types which supports key-value pairs.


Consider following example to create enumerated data types in Apex

Loops :

Apex supports five types of procedural loops. Following are the listed loops. All loops allows break and continue statement.

  • do{statement} while (condition);
  • while(conditionstatement;
  • for(initializationexit conditionincrementstatement;
  • for(variable : array/setstatement;
  • for(variable : [inline_soql_query]) statement;

Do-While Loops
Similar to other programming language ​ do-while loop repeatedly executes a block of code till the Boolean condition remains true. Its syntax is:

While Loops

The Apex​​ while loop executes a block of code until a particular Boolean condition remains true. Its syntax is:

For Loop:

Apex supports three variations of the for loop:

  • The traditional for loop:

  • The list or set iteration for loop:

  • The SOQL for loop:

Here both variable and variable_list must be of the same sObject type.

Implementation of Apex Trigger

Apex triggers works like stored procedures execute when a particular event occurs. One can write pre event and post event trigger. e.g.

Triggers are executed using the events like insert, update,delete, upsert, merge,undelete etc.

Example 1

Suppose we received a business requirement that we need to create an Payroll Record when Employee’s ‘Present Status’ field changes to present from absent. For this, we will create a trigger on APEX_Employee_c object by following steps:

Step 1: Go to sObject

Step 2: Click on Employee

Step 3: Click on ‘New’ button in Trigger related list and add the trigger code as give below.

Understanding & Use of Trigger Context Variables

All triggers use context variables which allows to access data at run time. These variables can be access thru Trigger class. Eg. Trigger,old, Trigger.isExecuting, Trigger.oldMap etc.



isExecutingIt returns true if the current context of the Apex code is a trigger
isInsertIt will return true if it is fired by insert operation
isUpdateIt will return true if it is fired by update operation
isDeleteIt will return true if it is fired by delete operation
isBeforeIt will return true if it is fired by before operation
isAfterIt will return true if it is fired by afterall operation
isUndeleteIt will return true if  date is recovered from recycle bin
newIt returns a list of the new versions of the sObject records.
newMapIt returns a map of IDs for  sObject records.
oldReturns a list of the old versions of the sObject records.
oldMapA map of IDs to the old map of the sObject records.
sizeThe total number of records in a trigger invocation, both old and new.

 Thats all for brief introduction on Apex. If you need to refer old tutorials please feel free to read them using below links

Tutorial Index

  1. Introduction to Cloud Computing ( and
  2. Overview of Database Concepts (
  3. Introduction to
  4. Building Salesforce Custom App and Objects
  5. Object Relationships and Formula Field in Salesforce
  6. Salesforce Security Model and Overview
  7. Automation in Salesforce
  8. Approval Process in Salesforce
  9. Introduction to SOQL and SOSL
  10. Introduction to Apex
  11. Salesforce Data Management
  12. Visualforce MVC Architecture on Cloud
  13. Salesforce Reports and Dashboards
  14. Building a Visualforce (Custom) Page for the Salesforce App
  15. Salesforce Sandbox and Overview of capabilities
  16. Learning Apex and Deployment Tools


Add a Comment

Your email address will not be published. Required fields are marked *