Apex Class Introduction
Apex is a strongly typed, object-oriented programming language which is a proprietary language developed by Salesforce.com to allow us to write the code that executes on the force.com platform. Apex is saved, compiled, and executed on the servers of the force.com platform. Apex helps developer to add business logic to most of the system events including button on-clicks, related record updates, and VF & lightning pages.
Features of Apex
1) Easy to Use
Apex is built on Java standard, such as variable and expression syntax, block and conditional statement syntax, loop syntax and array notation. Besides this Apex adds new features, it uses straightforward syntax and denotation to make efficient use of Lightning Platform.
2) Easy to test
Apex offers in-built support for unit test creation and execution, adopting test results that shows the details about the code coverage and informs the parts of code that can be organized well.
3) Integrated with the Database
It is Integrated with the Database which and it can access and manipulate records without the need to establish the DB connection.
4) Strongly typed Language
The Apex is a strongly typed language that directly references object-oriented schema, such as field names. If any references are faulty, it fails to compile quickly. It also maintains all dependencies in metadata.
5) Upgrades Automatically
As Apex is a part of the Salesforce platform, it upgrades automatically. So the developers don't have to worry about upgrading it manually.
Apex Data Types Overview
Apex supports various data types, including a data type specific to Salesforce—the sObject (Class Object) data type.
- A primary type, such as an Integer, Double, Long, Date, Date time, String, ID, Boolean, among others.
- An sObject, either as a generic sObject or as a specific sObject, such as an Account, Contact, or MyCustomObject__c
- A list (or array) of primary, sObjects, user defined objects, objects created from Apex classes, or collections
- A set of primary, sObjects, user defined objects, objects created from Apex classes, or collections
- map from a primitive to a primitive, sObject, or collection
- Typed list of values, also known as an enum
Apex Syntax and it's usage
Similarities between Object Oriented Programming Language (OOP) & Apex
- Classes, interfaces, properties, and collections (including arrays).
- Object and array notation.
- Expressions, variables, and constants.
- Conditional statements (if-then-else) and control flow statements (for loops and while loops).
What Apex supports in addition to Object Oriented Programming Language ?
- Apex is stored, compiled, and executed in the cloud. (Cloud Development)
- Triggers, which are similar to triggers in database systems.
- Database statements that allow you to make direct database calls and query languages to query and search data.
- Transactions and rollbacks.
- Versioning of custom code.
Now let's check out Apex Syntax and how to use it
Variable Declaration
As mentioned earlier, Apex is strongly types language so you need to declare a variable with data type
contact cont = new contact();
Here Variable cont is declared as a datatype with contact (Contact is Salesforce standard object).
SOQL Query
SOQL stands for Salesforce Object Query Language and it used for fetching object from Salesforce database
Let's take an example
Contact cnt = [select id, name from Contact Limit 1];
This query fetches one contact records from the Salesforce database.
Loop Statement
Loop statement helps iteration over the records. The number of iterations is equivalent to the number of records in the list same as in OOP Language
For Example:
list<Contacts> contactLst = [select id, name from contact limit 100]; // iteration over the list of accounts for(Contact cnt : contactLst){ //you can get contact id here this way cnt.Id }
Conditional Statement
When you execute some lines of code as per conditions, conditional statements are used. It is same as using If, Else If in OOP
Let's take an example
list<Account>listOfAccounts = [select id, name from account limit 100]; // execute the logic if the records returned are not empty (zero) if(listOfAccounts.size() > 0){ //your logic }
Data Manipulation (DML) Statements
DML statements are used for data manipulation in the Salesforce database.
Let's take an example
Contact acc = new Account(Name = ‘ Test Account’); Insert acc; //DML statement to create account record.
Types of Apex Access Specifiers
Apex provides the usage of the private, public, protected, and global access specifiers when defining variables or methods.
By default, a variable or method is private and only visible to the Apex code within the defining class.
You have to specify a variable as public to make it available to their classes in the same application namespace.
We can also change the visibility level by using the below access modifiers
Public
It offers access to a method, class, or variable that an Apex use within a namespace.
Private
This access specifier offers access to the method, class, and variable locally or within the code section in which it’s defined.
Protected
This access specifier provides access to a variable or method that any inner classes use within Apex class.
Global
It offers access to a method, class, or variable that an Apex use within and outside the namespace. It is a best practice not to use global keywords until required.
Keywords In Apex
Apex keywords, also known as reserved keywords, that act as a key to a code. Apex has defined these keywords as predefined words, so you can’t use them as an object name or variable as per the needs.
Below are the list of keywords in Apex
1. With Sharing Keyword
2. Without Sharing Keyword
3. Class
4. Virtual
5. Abstract
6. Interface
7. Extends
8. Implements
9. This
10. Static
11. Final
12. Super
13. Transient
14. Return
15. Null
16. Global
17. Public
What is Apex Governer Limits ?
The limits that the Apex runtime engine impose to make sure that any Apex code and processes don’t violate the processing and don’t control the shared resources for the other users on the multitenant environment
Such limits are authenticated against apex transactions.
Salesforce has defined various governor limits on every apex transaction, like
For SOQL queries that can be done in a synchronous transaction - Max Query Limit is 100
For SOQL queries that can be done in an Asynchronous transaction, etc. - Max Query Limit is 200
How to create an Apex Class in Salesforce?
1) Click on Setup Icon available in right hand side of page in Lightening
2) Enter Apex Classes in the quick find Box, then Click on Apex Classes
3) Click on New button present in Apex Classes Page, then you will see the window to add Apex Class Code
How can we help you ?
At Elitech Systems, We have a team of the best salesforce developers with the required skills that you need to accomplish your next Salesforce development project. We hold the required excellence and will help you maintain better relationships with your customers using the CRM platform tools.
Experienced Product Analyst with a demonstrated history of working in the information technology and product based industry. Skilled in Wireframing, Product Analysis, Customer Relationship Management (CRM), Ecommerce Platforms, HTML, Written Communication and requirement gathering.
Comments are closed.