Salesforce created Apex, a robust programming language that resembles Java, especially for modifying and improving Salesforce platform apps.
1. Goal: Custom business logic on the Salesforce platform is created using Apex. It is perfect for creating scalable, dynamic applications since it enables developers to carry out intricate procedures, automate chores, and carry out database activities within Salesforce.
2. Grammar: Developers accustomed to Java or C++ will find Apex relatively easy to learn because it shares similarities with Java in terms of grammar and object-oriented structure.
3. Execution Context: Apex shares resources with other users and apps on Salesforce’s servers since it operates in a multitenant environment. To ensure performance and security, Salesforce enforces governor limits, which restrict resource usage (e.g., memory, CPU time).
4. Features:
- Triggers: Apex lets you write code that runs automatically when certain events occur, such as the creation, updating, or deletion of a record.
- Classes and Controllers: Apex allows you to build custom classes and controllers that support Visualforce pages, Salesforce’s markup language for user interface design, and specific behavior.
- Batch Apex: Apex makes it possible to handle big datasets in batches without experiencing any issues.
- Integration: Apex can seamlessly integrate data by connecting Salesforce with external systems through API calls.
5. Security: Apex adheres to Salesforce’s built-in security features, which include field-level security and sharing rules. Additionally, developers can employ tools in their code to restrict user permissions.
Overall, Apex gives you the flexibility and power to customize Salesforce to meet specific business needs, enhancing productivity and enabling advanced functionality across Salesforce applications.
Basic syntax and structure in Apex to get you started
1. Comments
-
- Single-line: // This is a single-line comment
- Multi-line:
/*
This is a
multi-line comment
*/
2. Data Types
Integer, Double, Long, Decimal, Boolean, Date, Datetime, String, and ID.
Collections:
- Lists: List names = new List();
- Sets: Set uniqueNumbers = new Set();
- Maps: Map<Id, String> userNames = new Map<Id, String>();
3. Variables
Integer num = 10;
String name = ‘John Doe’;
4. Control Structures
If/Else Statement
if (num > 5) {
System.debug(‘Number is greater than 5’);
} else {
System.debug(‘Number is 5 or less’);
}
For Loop
for (Integer m = 0; m < 5; m++) {
System.debug('m: ' + m);
}
For Each Loop
List
for (String fruit : fruits) {
System.debug(fruit);
}
While Loop
Integer count = 0;
while (count < 5) {
System.debug(count);
count++;
}
5. Classes and Methods
Class Definition
public class MyClass {
// Member variables
public String name;
// Constructor
public MyClass(String name) {
this.name = name;
}
// Method
public void sayHello() {
System.debug(‘Hello, ‘ + name);
}
}
Creating Objects and Calling Methods
MyClass obj = new MyClass(‘John’);
obj.sayHello();
6. Trigger Syntax
Triggers are used to perform actions before or after certain database events.
trigger StudentTrigger on Student (before insert, after insert) {
for ( Student stu : Trigger.new) {
System.debug(Student Name: ‘ + stu.Name);
}
}
7. SOQL (Salesforce Object Query Language)
Query Salesforce data like SQL:
List
8. DML Operations
Insert
Student stu = new Student(Name=’New Student);
insert stu;
Update
stu.Name = ‘Updated Student;
update stu;
Delete
Delete stu;