Write Apex

APEX Tutorial: Apex Programming Class & Coding Examples

What is Apex in the context of Salesforce?
SalesForce Apex is an object-oriented and strongly typed programming language that was developed for the development of Software as as Service (SaaS) and Customer Relationship Management (CRM) applications (CRM). Providing back-end database support as well as client-server interfaces, Apex aids developers in the development of third-party SaaS applications as well as the incorporation of business logic into system events.

Apex enables developers to incorporate business logic into system events such as button clicks, related record updates, and Visualforce pages by using the Apex Object Model. Apex has a syntax that is similar to Java.

In this beginner Salesforce Apex tutorial, you will learn the fundamentals of Apex, including:

Apex is a language that does not care about case.

Using apex, you can perform DML operations on sObject records, such as INSERT, UPDATE, UPSERT, and DELETE on the records.

Salesforce object records can be queried using the SOQL (salesforce object query language) and SOSL (salesforce object search language) languages in Apex.

The ability to create unit tests and run them to verify the code coverage and efficiency of the code in Apex is provided by this feature.

Apex runs in a multi-tenant environment, and Salesforce has set some governor limits to prevent a user from having complete control over the shared resources in the environment. Any code that exceeds the salesforce governor limit will fail, and an error message will appear.

An object from Salesforce can be used as a datatype in Apex. As an illustration –

Account acc = new Account(); Account acc = new Account();

Account is a standard salesforce object in this case.

When a new Salesforce release is released, Apex automatically upgrades.

When Should a Developer Consider Using Apex?

The use of Apex code should only be considered if a business scenario is too complex to be implemented using the pre-built functionality offered by Salesforce.

The following are a few examples of situations in which we must write Apex code:

To develop web services that will allow Salesforce to communicate with other applications.

To implement custom validation on sobjects, see Custom Validation.

When a DML operation is performed, it is possible to run custom apex logic.

For the purpose of implementing functionality that cannot be achieved through the use of existing workflow flows and process builders features.

Email services must be configured to include processing the contents, headers, and attachments of emails using Apex code in order to be effective.

Apex’s Organizational Structure

During this Apex tutorial, we will learn about the working structure of Apex, which includes:

For an apex code, the following is the sequence of actions:

Developer Action: When a developer saves apex code to the platform, all of the apex code written by the developer is compiled into a set of instructions that can be understood by the apex runtime interpreter. These instructions are then saved to the platform as metadata.

After the user event has finished executing an apex code, the platform server retrieves the compiled instructions from the metadata and runs them through the apex interpreter before returning the result.

Apex’s Organizational Structure

Apex’s Organizational Structure

Syntax of the Apex

Declaration of Variables:

Because apex is a strongly typed language, it is necessary to declare a variable with a datatype in order for it to be used.

As an illustration,

new contact(); contact con = new contact();

The variable con is declared with the datatype contact as its data type.

Using SOQL, you can ask the following question:

Soql (salesforce object query language) is an abbreviation for salesforce object query language. The SOQL query language is used to retrieve sObject records from the Salesforce database. As an illustration-

Account acc = [select id and name from Account Limit 1]; Account acc = [select id and name from Account Limit 1]

The query above retrieves a record for an account from the salesforce database.

Statement about the loop:

In order to iterate over the records in a list, the loop statement is used. The number of times the list is iterated is the same as the number of records in the list. As an illustration, consider:

listAccount>listOfAccounts = [select id and name from account limit 100]; listAccount>listOfAccounts = [select id and name from account limit 100];

the loop iterates over the list of accounts;

the foreach(Account acc: listOfAccounts) statement is used to find the first account in the list of accounts.

the logic of your decision-making

}

The listOfAccounts variable in the preceding snippet of code has the datatype of list.

Declaratory statement about flow control:

When you want to execute some lines of code based on certain conditions, a flow control statement is extremely useful.

As an illustration, consider:

listAccount>listOfAccounts = [select id and name from account limit 100]; listAccount>listOfAccounts = [select id and name from account limit 100];

If the number of accounts in the list is greater than zero, then the logic is executed.

in which case(listOfAccounts.size() is greater than zero)

{

the logic of your decision-making

}

The snippet of code above is querying account records from the database and checking the size of the list of records returned.

Statement in DML format:

Data manipulation language (DML) is an abbreviation. In order to manipulate data in the Salesforce database, DML statements are used. As an illustration, consider the following:

The account acc is a new Account with the name “Test Account,” and the value of the account acc is the value of the account.

Insert acc; /DML statement to create a record for this account.

Apex Development Environment is a software development environment.

As a result of this tutorial, we will learn about the Apex Development Environment, which includes the following features:

Sandbox and developer editions of Salesforce are both available for developing Apex code.

Development of code in a sandbox environment and subsequent deployment of the code into the production environment is considered best practise.

Toolkits for developing Apex code includes the following: The following are the three tools that are available for developing apex code in all Salesforce editions:

Force.com Developer Console is a web-based application that allows developers to create custom applications for use in Force.com environments.

Force.com’s integrated development environment (IDE).

Salesforce’s User Interface includes a Code Editor.

You

Using Apex, you can type data.

Datatypes supported by apex include the ones listed below:

Primitive:

The primitive data types include integers, doubles, longs, dates, dates times, strings, IDs, and Booleans.

Not by reference, but by value, are all primitive data types passed between programmes.

Collections:

Apex provides three different types of collection.

The term “list” refers to an ordered collection of primitive objects (such as sObjects), collections, or Apex objects that are organised according to indices in the list.

In programming, a set is a collection of unique primitives that are not necessarily in any particular order.

There are many different types of maps, but the most common is a map of unique primitive keys mapping to single values that can be primitives, collections, or Apex objects. A map can be any type of object, including primitives, sObjects, and collections of primitive keys.

sObject:

Using Salesforce, this is a unique data type. It is analogous to a table in SQL, and it contains fields that are analogous to columns in SQL, among other similarities.

Enums

enum is an abstract data type that stores a single value for each of a finite set of identifiers that have been specified

Classes

Things to Look For

It is used to refer to any data type that is supported by Apex, including binary data.

Interfaces

access specifier for apex python

Access specifiers that are supported by Apex include the following:

Public:

Providing access to a class, method, or variable for use by an apex within the scope of a namespace is specified by this access specifier.

Private:

A class, method, or variable can be accessed using this access specifier, which allows them to be used locally or within the section of code where they are defined. Access specifiers are set to private by default for all technique and variable variables that do not have any defined access specifiers.

Protected:

A method or variable that is accessible through this access specifier can be used by any inner classes that are contained within the defining Apex class.

Global:

Providing access to a class, method, or variable for use by an apex both within and outside of a namespace is specified by this access specifier. It is recommended that global keywords are not used unless absolutely necessary in a particular situation.

Using Apex Keywords

As a result of the exchange of information:

The use of this keyword ensures that all sharing rules applicable to the current user are enforced, and the absence of this keyword ensures that code executes in the system context.

Using an illustration,

class that is open to the public MyApexClass{

When code in this class is executed, the sharing rules are enforced. /

}

Without divulging any information, please note that the following sentence is a paraphrase:

All of the sharing rules that apply to the current user are not enforced if a class is defined with this keyword.

Using an illustration,

with no need to disclose the classification MyApexClass{

When code in this class is executed, sharing rules are not enforced.

}

Description Limit
SOQL queries that can be done in a synchronous transaction 100
SOQL queries that can be done in an Asynchronous transaction 200
Records that can be retrieved by a SOQL query 50000
Records that can be retrieved by Database.getQueryLocator 10000
SOSL queries that can be done in an apex transaction 20
Records that can be retrieved by a SOSL query 2000
DML statements that can be done in an apex transaction 150
Records that can be processed as a result of a DML statement, Approval.process, or database.emptyRecycleBin 10000
Callouts that can be done in an apex transaction. 100
Cumulative timeout limit on all the callouts that are being performed in an apex transaction 120 seconds
Limit on apex jobs that can be added to the queue with System.enqueueJob 50
Execution time limit for each Apex transaction 10 minutes
Limit on characters that can be used in an apex class and trigger 1 million
CPU time limit for synchronous transaction 10,000 milliseconds
CPU time limit for asynchronous transaction 60,000 milliseconds