What is Apex?
- Apex is strongly typed object-oriented, on-demand programming language. It is compiled, stored, and run entirely on the Force.com platform (multi-tenant environment and is very controlled in its invocations and limits).
- Apex syntax looks mostly like Java and acts like stored procedures.
- Apex allows developers to attach business logic to the record save process
- Apex has built-in support for unit test creation and execution.
As a language apex is Integrated, Easy to use, Data focused, Rigorous, Hosted, Multi-tenant aware, automatically up-gradable, easy to test and versioned.
Below image helps you to understand how apex works:
When developers write & save code in the platform, which is compiled on force.com platform and stores in the form of metadata in Salesforce servers. End users can send request from UI & retrieve results from Salesforce servers.
Below are few capabilities of Apex
- Apex provided build in support for:
- DML calls to insert, update, and delete a record.
- Inline SOQL or SOSL statements for retrieving records.
- Looping control structures that help with bulk processing.
- A record locking syntax that prevents record update conflicts.
- Custom public API calls.
- Send and receive emails.
- Web services or XML request/response integrations.
- Warnings and errors to prevent objects referenced by Apex from being modified.
Difference between traditional code & Apex programming
- A traditional code is fully flexible and can tell the system to do anything.
- Apex is governed, can only do what the system allows.
What is the apex class and Triggers?
Apex Classes: It is a collection of variables and a library of methods that can be reused.
Apex Trigger: It is a script that executes before or after a specific data manipulation language (DML) event on a particular Salesforce object.
When can we use Apex programming?
Apex should be used as a solution when:
- You need to apply complex business logic to rows of data being saved by any means.
- You need to create additional web services API functionality for exposing logic either within Salesforce or to external applications.
- You need to call out to external Web service and process the results.
- You need to handle incoming or outgoing emails in ways more complex than the declarative functionality.
Apex triggers execute no matter how the triggering data is being saved.
Apex executes regardless of whether the action originates in the user interface, through AJAX toolkit, or from web services API.
If you only want the code to execute through UI, consider making a Visualforce page and controller.