Platform Fundamentals: Apex

Ashish KothariOfferings, Salesforce, Technical TipsLeave a Comment Platform Fundamentals: Apex

What is Apex?

Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the platform server in conjunction with calls to the​ API. Using syntax that looks like Java and acts like database stored procedures, Apex enables 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 vs. Java

They are both similar in syntax and notation, strongly typed and transactional. Both support OOP features viz. Encapsulation, Inheritance, Polymorphism.

Apex runs in a Multi-Tenanted environment and is limited by Governor limits. It is case-sensitive and compiled on-demand. It is a platform proprietary language used specifically for business functions and requires unit testing for deployment.

Apex Class vs. Apex Triggers

An Apex Class is a library of attributes and methods and serves as a blueprint to create Apex objects. An Apex Class is similar to a Java Class and it can implement multiple interfaces.

An Apex Trigger is a script that executes before a specific event. An Apex Trigger can be associated with both a Standard Object and a Custom Object and executes as soon as an Object is saved.

Apex is invoked in one of the following ways:

  • Triggers are executed when data is saved through the UI or the API
  • By creating a job through the Apex Scheduler
  • Through VF controllers which provide data and actions to VF pages
  • Through Web Services API for classes exposed as Web Services
  • Through anonymous code blocks which are used for testing and debugging


When do you use Apex Triggers?

  • When you want to execute logic and perform some action when a record is deleted because validation rules only execute when a record is created or edited
  • Reuse existing interfaces by querying external Data Source(s) via web services and verify that business rules are being met. The Apex Trigger can then update the Data Source to keep the systems in sync
  • When you want to create a custom web service that wraps updates to multiple objects in a transaction or executes complex business logic
  • To get around limitations of Workflow Rules by automating field updated to unrelated records
  • To automate the creation or the deletion of a new record
  • To execute complex actions that write to multiple objects when a button or a link is clicked
  • To identify duplicate records and merge the results or to move existing records to a new object
  • To create a custom email service to handle inbound emails


As a language, Apex is:

[acc_item title=”Integrated“]Apex provides built-in support for common platform idioms, including:- Data manipulation language (DML) calls, such as INSERT, UPDATE, and DELETE- Inline Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) queries that return lists of sObject records[/acc_item] [acc_item title=”Data focused“]Apex is designed to thread together multiple query and DML statements into a single unit of work on the platform server, much as developers use database stored procedures to thread together multiple transaction statements on a database server[/acc_item] [acc_item title=”Rigorous“]Apex is a strongly-typed language that uses direct references to schema objects such as object and field names. It fails quickly at compile time if any references are invalid, and stores all custom field, object, and class dependencies in metadata to ensure they are not deleted while required by active Apex code[/acc_item] [acc_item title=”Hosted“]Apex is interpreted, executed, and controlled entirely by the platform[/acc_item] [acc_item title=”Multi-Tenant aware“]Like the rest of the platform, Apex runs in a multi-tenant environment. Consequently, the Apex runtime engine is designed to guard closely against runaway code, preventing them from monopolizing shared resources. Any code that violate these limits fail with easy-to-understand error messages[/acc_item] [acc_item title=”Automatically upgradeable“]Apex never needs to be rewritten when other parts of the platform are upgraded[/acc_item] [acc_item title=”Easy to test“]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 could be more efficient[/acc_item] [acc_item title=”Versioned“]You can save your Apex code against different versions of the API[/acc_item]

[message_box title=” Certified” color=”blue”]From the small business to the large enterprise, we have solutions to help improve win rates.[/message_box] Contact Us

Leave a Reply

Your email address will not be published.