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.

Features of Apex as a Language

  • 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
  • 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.

Flow of Actions:

There are two sequence of actions when the developer saves the code and when an end user performs some action which invokes the Apex code as shown below:

  1. Developer Action: When a developer writes and saves Apex code to the platform, the platform application server first compiles the code into a set of instructions that can be understood by the Apex runtime interpreter, and then saves those instructions as metadata.
  2. End User Action: When an end-user triggers the execution of Apex, by clicking a button or accessing a Visualforce page, the platform application server retrieves the compiled instructions from the metadata and sends them through the runtime interpreter before returning the result. The end-user observes no differences in execution time as compared to the standard application platform request.

Since Apex is the proprietary language of Salesforce.com, it does not support some features which a general programming language does. Following are a few features which Apex does not support:

  • It cannot show the elements in User Interface.
  • You cannot change the standard SFDC provided functionality and also it is not possible to prevent the standard functionality execution.
  • Temporary file creation is not supported.
  • Creating multiple threads is also not possible as we can do it in other languages.




Working Structure of Apex Diagram.


Understanding the Apex Syntax

  • Variable Declaration: As strongly typed language, you must declare every variable with data type in Apex. As seen in the code below (screenshot below), lstAcc is declared with data type as List of Accounts.
  • SOQL Query: This will be used to fetch the data from Salesforce database. The query shown in screenshot below is fetching data from Account object.
  • Loop Statement: This loop statement is used for iterating over a list or iterating over a piece of code for a specified number of times. In the code shown in the screenshot below, iteration will be same as the number of records we have.
  • Flow Control Statement: The If statement is used for flow control in this code. Based on certain condition, it is decided whether to go for execution or to stop the execution of the particular piece of code. For example, in the code shown below, it is checking whether the list is empty or it contains records.
  • DML Statement: Performs the records insert, update, upsert, delete operation on the records in database. For example, the code given below helps in updating Accounts with new field value.

Static and Instance Methods, Variables, and Initialization Code

In Apex, you can have static methods, variables, and initialization code. However, Apex classes can't be static. You can also have instance  methods, member variables, and initialization code, which have no modifier, and local  variables.

  • Characteristics:
    • Static methods, variables, and initialization code have these characteristics
      • They’re associated with a class.
      • They’re allowed only in outer classes.
      • They’re initialized only when a class is loaded.
      • They aren’t transmitted as part of the view state for a Visualforce page.
    • Instance methods, member variables, and initialization code have these characteristics
      • They’re associated with a particular object.
      • They have no definition modifier.
      • They’re created with every object instantiated from the class in which they’re declared.
    • Local variables have these characteristics:
      • They’re associated with the block of code in which they’re declared.
      • They must be initialized before they’re used.

Keywords

Apex provides the keywords final, instanceof, super, this, transient, with sharing and without sharing Check on Given Links:

  1. Using the final Keyword
  2. Using the instanceof Keyword
  3. Using the super Keyword
  4. Using the this Keyword
  5. Using the transient Keyword
  6. Using the with sharing, without sharing, and inherited sharing Keywords

Annotations

Apex supports the following annotations:

Working with Data in Apex

You can add and interact with data in the Lightning Platform persistence layer. The sObject data type is the main data type that holds data objects. You’ll use Data Manipulation Language (DML) to work with data, and use query languages to retrieve data, such as the (), among other things.

  • Working with sObjects : In this developer guide, the term  refers to any object that can be stored in the Lightning platform database.
  • Data Manipulation Language : Apex enables you to insert, update, delete or restore data in the database. DML operations allow you to modify records one at a time or in batches.
  • SOQL and SOSL Queries : You can evaluate Salesforce Object Query Language (SOQL) or Salesforce Object Search Language (SOSL) statements on-the-fly in Apex by surrounding the statement in square brackets.
  • SOQL For Loops : SOQL  loops iterate over all of the sObject records returned by a SOQL query.
  • sObject Collections : You can manage sObjects in lists, sets, and maps.
  • Dynamic Apex
  • Apex Security and Sharing : When you use Apex, the security of your code is critical. You'll need to add user permissions for Apex classes and enforce sharing rules. Read on to learn about Apex managed sharing and get some security tips.
  • Custom Settings : Custom settings are similar to custom objects. Application developers can create custom sets of data and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. Formula fields, validation rules, flows, Apex, and SOAP API can then use this data.

Debugging, Testing, and Deploying Apex

Develop your Apex code in a sandbox and debug it with the Developer Console and debug logs. Unit-test your code, then distribute it to customers using packages

  • Debugging Apex : Apex provides debugging support. You can debug your Apex code using the Developer Console and debug logs.
  • Testing Apex : Apex provides a testing framework that allows you to write unit tests, run your tests, check test results, and have code coverage results.
  • Deploying Apex : You can't develop Apex in your Salesforce production org. Your development work is done in either a sandbox or a Developer Edition org.
  • Distributing Apex Using Managed Packages : As an ISV or Salesforce partner, you can distribute Apex code to customer organizations using packages. Here we'll describe packages and package versioning.