Apex Programming for Beginners
Introduction to Apex Class and Trigger
What is Apex Programming and why is it required for customizing Salesforce?
Apex is an object oriented Programming language, it is stored, compiled and run on force.com platform and is, therefore, on-demand programming language.
Apex’s syntax is similar to Java, they behave similar to a stored procedure, and the logic written in Apex can be linked to the record save procedure. Salesforce’s apex provides features for unit test creation and execution.
What are the capabilities of Apex?
Apex help in programmatic access to the exposed Salesforce.com feature. The list of these features is mentioned below.
Actions, Approval process, Authentication, chatter, communities, email, metadata, Salesforce knowledge, Salesforce connect, territory management, visual workflow etc.
What are Apex transactions and Governor limit?
Salesforce has made its foundation based on multi-tenant architecture, to ensure the resources are used efficiently, Salesforce introduced the governor execution limit. Following terms are used together when it comes to talk about Governor limits.
Mostly Governor limits are applicable per Apex Transactions basis, but some are on rolling 24 hours basis. Apex transaction is the execution of a set of operation as one unit, boundary of the transaction can be a trigger, anonymous code or a web service etc.
Execution Governors and Limits- since Salesforce is in a multitenant environment, its servers ensures that a single apex code does not monopolize all the resource and keeps a check on all the execution and these checks are known as governor limits.
Running Apex within Governor Execution Limits- sometimes there arises a need or requirement which wants us to execute code which will breach the governor limit. In those circumstances, to adhere to these limits certain design patterns are used, i.e. bulk calls and relationship queries.
Getting started with Coding in Apex
Before we proceed to deep dive into the code we need to know certain fundamentals of Apex.
As we mentioned earlier, the syntax of Apex is similar to java, but there are other fundamentals as well which are quite similar, below are the features similar to Java.
Data Types and Variables
Control Flow Statements
Classes, Objects, and Interfaces
Working with Data in Apex
Data Types and variables
All variable in apex have a data type, it is basically an alias for a memory location allocated to hold a value. For eg. sObject, integer, enum etc.
Then Salesforce has primitive data type, integer string, double etc. It also has collections namely list, set and Map.
Control Flow Statements
To control the flow of statements, apex provides if-else and loops.
Conditional (If-Else) Statements
Apex is presently supporting 5 types of loops, namely: while loop, do while loop, for loop, for-each loop
For loop with SOQL as an argument.
Objects interface and classes
Similar to Java, Apex is also based on the concept of Classes, interface, and objects. So what is the difference between a Java class and Apex Class, main differences are mentioned below:
- Classes and methods are final by default.
- Default access modifier is private, i.e. specifying no access modifier and declaring privately is synonymous in Apex.
- An interface is always global, no other access modifiers are allowed.
- Inner class and interface are allowed only one level deep, inner class behaves like static without specifying it as static.
How to create a class?
Salesforce has provided us with the Apex and Visualforce editor in the UI. There are many ways to reach the editor.
Kindly note that Apex class and triggers can only be created in either sandbox or developer edition, but Visualforce is allowed to be created at the production environment.
Apex Class editor has a help text inbuilt, this helps in auto completion of the syntax, which save a lot of time.
Now with all the Bookish knowledge which is available on the internet, we should now proceed to write some code.
Next : Working with Data