SF Unit-V
SF Unit-V
What is Apex?
Apex is a proprietary language developed by the Salesforce.com. As per the official definition, Apex is a strongly typed, object-oriented programming language that
allows developers to execute the flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.
It has a Java-like syntax and acts like database stored procedures. It enables the developers to add business logic to most system events, including button clicks,
related record updates, and Visualforce pages.Apex code can be initiated by Web service requests and from triggers on objects. Apex is included in Performance
Edition, Unlimited Edition, Enterprise Edition, and Developer Edition.
Integrated
Apex has built in support for DML operations like INSERT, UPDATE, DELETE and also DML Exception
handling. It has support for inline SOQL and SOSL query handling which returns the set of sObject records.
We will study the sObject, SOQL, SOSL in detail in future chapters.
Java like syntax and easy to use
Apex is easy to use as it uses the syntax like Java. For example, variable declaration, loop syntax and
conditional statements.
Strongly Integrated With Data
Apex is data focused and designed to execute multiple queries and DML statements together. It issues multiple
transaction statements on Database.
Strongly Typed
Apex is a strongly typed language. It uses direct reference to schema objects like sObject and any invalid
reference quickly fails if it is deleted or if is of wrong data type.
Multitenant Environment
Apex runs in a multitenant environment. Consequently, the Apex runtime engine is designed to guard closely
against runaway code, preventing it from monopolizing shared resources. Any code that violates limits fails
with easy-to-understand error messages.
Upgrades Automatically
Apex is upgraded as part of Salesforce releases. We don't have to upgrade it manually.
Easy Testing
Apex provides built-in support for unit test creation and execution, including test results that indicate how
much code is covered, and which parts of your code can be more efficient.
Like other object-oriented programming languages, these are some of the language constructs
that Apex supports:
Only one object can be searched at a time Many objects can be searched at a time
Can query any type of field Can query only on email, text or phone
Can be used in classes and triggers Can be used in classes, but not triggers
An Apex transaction represents a set of operations that are executed as a single unit. The
operations here include the DML operations which are responsible for querying records. All
the DML operations in a transaction either complete successfully, or if an error occurs even
in saving a single record, then the entire transaction is rolled back.
Use sObjects
Because Apex is tightly integrated with the database, you can access Salesforce records and
their fields directly from Apex. Every record in Salesforce is natively represented as
an sObject in Apex. For example, the Acme account record corresponds to an Account
sObject in Apex. The fields of the Acme record that you can view and modify in the user
interface can be read and modified directly on the sObject as well.
The following table lists some populated fields of the Acme account example record. The
Account sObject is an abstraction of the account record and holds the account field
information in memory as an object.
If you’ve added custom objects in your organization, use the API names of the custom
objects in Apex. For example, a custom object called Merchandise corresponds to the
Merchandise__c sObject in Apex.
The names of sObjects correspond to the API names of the corresponding standard or custom
objects. Similarly, the names of sObject fields correspond to the API names of the
corresponding fields.
API names of object and fields can differ from their labels. For example, the Employees field
has a label of Employees and appears on the account record page as Employees but its API
name is NumberOfEmployees. To access this field in Apex, you’ll need to use the API name
for the field: NumberOfEmployees.
The following are highlights of some rules used for API names for custom objects and
custom fields.
For custom objects and custom fields, the API name always ends with the __c suffix. For
custom relationship fields, the API name ends with the __r suffix. For example:
In addition, spaces in labels are replaced with underscores in API names. For example, a
custom field name of Employee Seniority has an API name of Employee_Seniority__c.
To read a record from Salesforce, you must write a query. Salesforce provides the Salesforce
Object Query Language, or SOQL in short, that you can use to read saved records. SOQL is
similar to the standard SQL language but is customized for the Lightning Platform.
Because Apex has direct access to Salesforce records that are stored in the database, you can
embed SOQL queries in your Apex code and get results in a straightforward fashion. When
SOQL is embedded in Apex, it is referred to as inline SOQL.
To include SOQL queries within your Apex code, wrap the SOQL statement within square
brackets and assign the return value to an array of sObjects. For example, the following
retrieves all account records with two fields, Name and Phone, and returns an array of
Account sObjects.
Copy
The WHERE clause is optional. Let’s start with a very simple query. For example, the
following query retrieves accounts and gets Name and Phone fields for each account.
Copy
1. SELECT Name,Phone: This part lists the fields that you would like to retrieve. The
fields are specified after the SELECT keyword in a comma-delimited list. Or you can
specify only one field, in which case no comma is necessary (e.g. SELECT Phone).
2. FROM Account: This part specifies the standard or custom object that you want to
retrieve. In this example, it’s Account. For a custom object called Invoice_Statement,
it is Invoice_Statement__c.
Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to
perform text searches in records. Use SOSL to search fields across multiple standard and
custom object records in Salesforce. SOSL is similar to Apache Lucene.
Adding SOSL queries to Apex is simple—you can embed SOSL queries directly in your
Apex code. When SOSL is embedded in Apex, it is referred to as inline SOSL.
This is an example of a SOSL query that searches for accounts and contacts that have any
fields with the word 'SFDC'.
Like SOQL, SOSL allows you to search your organization’s records for specific information.
Unlike SOQL, which can only query one standard or custom object at a time, a single SOSL
query can search all objects.
Another difference is that SOSL matches fields based on a word match while SOQL performs
an exact match by default (when not using wildcards). For example, searching for 'Digital' in
SOSL returns records whose field values are 'Digital' or 'The Digital Company', but SOQL
returns only records with field values of 'Digital'.
SOQL and SOSL are two separate languages with different syntax. Each language has a
distinct use case:
SearchQuery is the text to search for (a single word or a phrase). Search terms can be
grouped with logical operators (AND, OR) and parentheses. Also, search terms can include
wildcard characters (*, ?). The * wildcard matches zero or more characters at the middle or
end of the search term. The ? wildcard matches only one character at the middle or end of the
search term.