Apex is a proprietary language developed by Salesforce.com. It is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API. Basic knowledge of the Salesforce platform and development is needed. Apex is a programming language that has to be used with Salesforce.
In this Apex Tutorial, you will learn
Introduction to APEX
Features of Apex
Working Structure of Apex
Apex Syntax
Data Type in Apex
Apex Development Environment
Keywords in Apex
Apex Class
Introduction to APEX
Apex is a programming language developed by the Salesforce.com. As per the official definition, Apex is a strongly typed, object-oriented programming language that allows developers to implement the flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.
Apex has a Java-like syntax and it acts like database stored procedures. It enables the developers to add business logic to most system events, including button clicks, related record updates, and Visual force pages. Apex Code can be established by Web service requests and from triggers on objects. It has included in Performance Edition, Unlimited Edition, Enterprise Edition, and Developer Edition.
Features of Apex
Here are important features of Apex:
- Apex is a case insensitive language.
- You can perform DML operations like INSERT, UPDATE, UPSERT, and DELETE on sObject records using apex.
- You can query sObject records using SOQL (Salesforce object query language) and SOSL (Salesforce object search language) in apex.
- It allows you to create a unit test and execute them to verify the code coverage and efficiency of the code in apex.
- It executes in a multi-tenant environment, and Salesforce has defined some governor limits that prevent a user from controlling the shared resources. Any code that crosses the salesforce governor limit fails, an error shows up.
- Salesforce object can be used as a datatype in apex. For example –
Account acc = new Account ();
Here Account is a standard salesforce object.
- Apex automatically upgrades with every
Salesforce release.
Working Structure of Apex
Following are the flow of actions for an apex code:
- Developer Action: All the apex code written by a developer is compiled into a set of instructions that can be understood by an apex runtime interpreter when the developer saves the code to the platform and these instructions then it save as metadata to the platform.
- End-User Action: When the user event executes an apex code, the platform server gets the compiled instructions from metadata and runs them through the apex interpreter before returning the result.
Apex Syntax
Variable Declaration: As apex is strongly typed language, it is mandatory to declare a variable with data type in apex.
For example
contact con = new contact();
here the variable con is declared with contact as a datatype.
SOQL Query: SOQL stands for salesforce object query language. SOQL is used to fetch sObject records from the Salesforce database. For example-
Account acc = « ______ »;
The above
query fetches the account record from the Salesforce database.
Loop Statement: A
loop statement is used to iterate over the records in a list. The number of
iteration is equal to the number of records in the list. For example:
list<Account>listOfAccounts =
« ______ »;
// iteration over the list of accounts
for(Account acc : listOfAccounts){
//your logic
}
In the above snippet of code, listOfAccounts is a variable
of list datatype.
Flow Control
Statement: Flow control statement is beneficial when you want to execute
some lines of the code based on some conditions.
For example:
list<Account>listOfAccounts
= « ______ »;
// execute the logic if the size of
the account list is greater than zero
if(listOfAccounts.size() >0){
//your logic
}
The above snippet of code is querying account records from
the database and checking the list size.
DML statement: DML
stands for data manipulation language. DML statements are used to manipulate
data in the Salesforce database. For example –
Account acc = new Account(Name = ‘
Test Account’);
Insert acc; //DML statement to create account record.
Data Type in Apex
Following are the data types supported by apex:
Primitive: Integer,
Double, Long, Date, Date-Time, String, ID, and Boolean are considered as
primitive data types. All primitive data types are passed by value but not by reference.
Collections: Three
types of collection are available in Apex
- List:
It is an ordered collection of primitives, sObjects, collections, or Apex
objects based on indices. - Set:
An unordered collection of unique primitives. - Map:
It is a collection of unique, primitive keys that map to single values which
can be primitives, sObjects, collections, or Apex objects.
Object: This is a
special data type in Salesforce. It is similar to a table in SQL and contains
fields that are similar to columns in SQL.
Enums: It is an
abstract data type that stores one value of a finite set of specified
identifiers
Classes
Objects: Object
refers to any data type which is supported in Apex.
Interfaces
Apex Development Environment
Apex Code can be developed either in the sandbox and
developer edition of Salesforce.
It is a best practice to develop the code in the sandbox environment and then deploy it to the production environment.
Apex code development tools: The following are the three tools available to develop apex code in all editions of Salesforce.
- Force.com Developer Console
- Force.com IDE
- Code Editor in the Salesforce User InterfaceYou
Keywords in Apex
- With sharing
- Without sharing
- Static
- Final
- Return
- Null
- Abstract
Apex Class
An apex class is a blueprint or template from which the objects are created. An object is the instance of a class.
These are three ways of creating apex classes in Salesforce:
- Developer Console
- Force.com IDE
- Apex class detail page
In apex, you can define an outer class also called top-level class, and you can also define classes within an outer class called inner classes.
It is mandatory to use an access modifier like global or public in the declaration of the outer class.
It is not necessary to use an access modifier in the declaration of inner classes.
An apex class is defined using a class keyword followed by the class name.
Extends keyword is used to extend the existing class by an apex class, and implements keyword is used to implement the interface by an apex class.
Salesforce apex doesn’t support multiple inheritances; an apex class can only extend one existing apex class but can implement multiple interfaces.
An apex class can contain a user-defined constructor, and if a user-defined constructor is not available, a default constructor is used. The code in a constructor executes when an instance of a class is created.
Syntax of an apex class:
public class myApexClass{
// variable declaration
//constructor
public myApexClass{
}
//methods declaration
}
The new keyword is used to create an instance of an apex class. This is the syntax for creating an instance of an apex class.
myApexClass obj = new myApexClass();