In this article, we will discuss about Apex, Apex vs other languages, different ways of executing Apex, how to save and compile Apex and Governor Limits.
- Definition of Apex –
- Apex is strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce server in conjunction with call 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 page.
- Apex code can be initiated by web service request and from triggers on objects.
- Salesforce platform itself is written in Java, hence Apex is similar to Java.
- Apex vs Other Languages –
- Like C# & Java, Apex is also strongly-typed programming language.
- As like Java & C#, which compile into some intermediatory code, which is run by virtual machine, Apex also when compiled can be interpreted by Apex runtime interpreter.
- Like C# & Java, Apex also deals with garbage handling and memory allocation.
- Syntax structure of Apex is similar to Java and C#.
- Unlike Java & C#, which can be executed anywhere (desktop/sever/cloud/mobile phone), Apex can only be executed and run on the Salesforce platform and in its cloud.
- Another difference between Apex and other platforms is, the requirement that Apex code deployed to a live production environment must have 75% code coverage from unit tests.
- Executing Apex –
- Anonymous Blocks:
- Apex can be executed as an anonymous block of code, that is block of code that is merely compiled and executed by Salesforce platform and not stored as metadata for use at later date.
- It is executed through the Execute Anonymous window in developer console, but can also be executed through API.
- Triggers:
- Triggers in Salesforce is similar to triggers in other database language & can be fire both before and after operations.
- Trigger can invoke Apex to run when a record is changed within the system.
- Controllers (Visualforce/Lightning & JavaScript):
- Although the syntax and ways of invoking the Apex may be different, essentially, these all operate the same way to allow communication between the user via front-end and the Salesforce backed.
- Asynchronous Apex –
- For longer running tasks that do not need immediate execution, Apex has number of Asynchronous execution operations.
- Batch Apex for processing large data volumes.
- Future method for long-running operations or callouts.
- Queueable Apex for process that you may want to chain or monitor.
- Scheduled Apex for operations that should fire at a particular time or particular date.
- Web Services –
- Apex code can be exposed as a web-service for both SOAP & REST based APIs that can be called from external systems using the Salesforce AJAX toolkit.
- Email Services –
- You can assig Apex classes to handle incoming emails to specific addresses that can be processed to interact with the Salesforce database.
- Anonymous Blocks:
- Saving and compiling Apex –
- Except anonymous blocks, all Apex is saves on the Salesforce platform for execution when invoked in appropriate manner.
- If error occurs while compilation/failure, the Apex being saved is not updated on the database and the last compiled version remails.
- Governor Limits –
- Because, Salesforce is a multi-tenant environment, resources have to be shared by user in a consistent way that ensure no user causes a performance issue for other users on platform.
- Salesforce manages this through the use of Governor Limits.
Thank you for reading till end, in next article, we will discuss about Variables in Apex.
Leave a comment