APEX in Salesforce
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com 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 pages. Apex code can be initiated by Web service requests and from triggers on objects.
Apex provides built-in support for common Force.com platform idioms, including:
1. Data manipulation language (DML)
- Soql and Sosl (To return records and search records)
- Looping that allows for bulk processing of multiple records at a time
- Custom public Force.com API calls that can be built from stored Apex methods
- Easy to use
- Data focused
- Multi-tenant aware
- Easy to test
How Does Apex Work?
Where to write Apex?
- The com IDE is a plug-in for the Eclipse IDE
- For a trigger on a standard object, from Setup, click Customize, click the name of the object, and click Triggers. In the Triggers detail page, click New and then enter your code in the Body text box.
- For a trigger on a custom object, from Setup, click Develop Objects, and click the name of the object. In the Triggers related list, click New, and then enter your code in the Body text box.
- For a class, from Setup, click Develop Apex Classes. Click New, and then enter your code in the Body text box.
Note: You cannot make changes to Apex using the Salesforce user interface in a Salesforce production organization.
When Should I Use Apex?
- Create Web services.
- Create email services.
- Perform complex validation over multiple objects.
- Create complex business processes that are not supported by workflow
The following language constructs form the base parts of Apex:
- Data Types
- Assignment Statements
- Conditional (If-Else) Statements
- SOQL and SOSL Queries
- Exception Statements
Primitive Data Types:
- Apex primitive datatypes include:
Apex uses the same primitive data types as the SOAP API. All primitive data types are passed by value
Local variables are declared with Java-style syntax. For example:
- Note: Multiple Variable can initialized in a single statement Integer i,j,k;
Apex is Case In Sensitive.
- Variable and method names are case insensitive.
- References to object and field names are case insensitive.
- SOQL and SOSL statements are case insensitive.
Account accts = [Select ID From Account where Name=’salesforce’]
Apex uses exceptions to note errors and other events that disrupt the normal flow of code execution. Throw statements can be used to generate exceptions, while try, catch , and finally can be used to gracefully recover from an exception.
Code block ;//Dml Insert
Code block;//Dml Exception