Trainigs 22
Trainigs 22
Apex Programming
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.
Features of Apex Programming Language
Here are the important features of Salesforce Apex:
Apex is a case insensitive language.
You can perform DML operations like INSERT, UPDATE, UPSERT, 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.
Allows you to create a unit test and execute them to verify the code coverage and efficiency of
the code in apex.
Apex 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.
Apex Syntax
Variable Declaration: contact con = new contact();
SOQL Query: Account acc = [select id, name from Account Limit
1];
Loop Statement:
list<Account>listOfAccounts = [select id, name from account limit
100]; // iteration over the list of accounts for(Account acc :
listOfAccounts){ //your logic }
Flow Control Statement: list<Account>listOfAccounts = [select
id, name from account limit 100]; // execute the logic if the size of
the account list is greater than zero if(listOfAccounts.size() >0){
//your logic }
DML statement: Account
acc = new Account(Name = ‘ Test Account’); Insert acc; //DML
statement to create account record.
Data Type in 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, 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 collection of unique, primitive keys that map to single values
which can be primitives, sObjects, collections, or Apex objects.
sObject: This is a special data type in Salesforce. It is similar to a table in
SQL and contains fields which are similar to columns in SQL.
Enums Enum is an abstract data type that stores one value of a finite set
of specified identifiers
Classes
Objects : It refers to any data type which is supported in Apex.
Apex Access Specifier
Public:
This access specifier gives access to a class, method, variable to be used by
an apex within a namespace.
Private:
This access specifier gives access to a class, method, variable to be used
locally or within the section of code, it is defined. All the technique, variables
that do not have any access specifier defined have the default access specifier
of private.
Protected:
This access specifier gives access to a method, variable to be used by any
inner classes within defining Apex class.
Global:
This access specifier gives access to a class, method, variable to be used by
an apex within a namespace as well as outside of the namespace. It is a best
practice not to used global keyword until necessary.
Keywords in Apex
With sharing: If a class is defined with this keyword, then
all the sharing rules apply to the current user is enforced and
if this keyword is absent, then code executes under system
context.
For Example: public with sharing class MyApexClass{ //
sharing rules enforced when code in this class execute }
Without sharing: If a class is defined with this keyword,
then all the sharing rules apply to the current user is not
enforced.
For Example: public without sharing class MyApexClass{ //
sharing rules is not enforced when code in this class
execute }
QA