Writing Business Logic with the Apex Programming Language for the Force.com Platform
Business logic in Force.com is developed in Apex, a programming language designed for the Force.com platform. Through Apex code, many platform features, such as the database and user interface, can be customized to meet the needs of individual users and companies.
This chapter introduces Apex as a language for writing business logic, specifically where it interacts with the Force.com database. It uses a combination of explanatory text and code snippets to introduce concepts and encourage experimentation. This approach assumes you’re already experienced in some other high-level, object-oriented programming language and would like to see for yourself how Apex is different.
The chapter consists of the following sections:
- Introduction to Apex—Learn basic facts about Apex and how it differs from other programming languages.
- Introducing the Force.com IDE—Take a brief tour of the Force.com IDE, a user interface for developing, debugging, and testing Apex code.
- Apex language basics—Learn the building blocks of the Apex language, such as data types and loops.
- Database integration in Apex—Incorporate the Force.com database into your Apex programs through queries, statements that modify data, and code executed automatically when data is changed.
- Debugging Apex using Developer Console—With Developer Console, you can directly inspect the state of your Apex code as it runs.
- Unit tests in Apex—Write tests for your code and run them in Developer Console.
- Sample application—Walk through the implementation of a data validation rule for the Services Manager sample application.
Introduction to Apex
Apex is a stored procedure-like language that runs entirely on the Force.com platform. It provides object-oriented features and tight integration with the Force.com database. It’s mainly used in custom user interfaces and in triggers, code that is executed when data is changed in the database.
Apex is not a general-purpose programming language like Java or C. Its scope is limited to business and consumer applications that operate on relational data and can benefit from the feature set of the surrounding Force.com platform.
Apex programs exist in a multitenant environment. The computing infrastructure used to execute Apex is operated by Salesforce and shared among many developers or tenants of the system. As a result, unlike general-purpose programming languages you are familiar with, the execution of Apex programs is closely controlled to maintain a consistently high quality of service for all tenants.
This control is accomplished through governor limits, rules that Force.com places on programs to keep them operating within their allotted share of system resources. Governor limits are placed on database operations, memory and bandwidth usage, and lines of code executed. Some governor limits vary based on the type of licensing agreement you have in place with Salesforce or the context that the code is running in, and others are fixed for all users and use cases.
Here are a few important facts about Apex:
- It includes integrated testing features. Code coverage is monitored and must reach 75% or greater to be deployed into a production environment.
- It is automatically upgraded. Salesforce executes all of its customers’ unit tests to verify that they pass before deploying a major release of the Force.com platform. Your code is always running on the latest version of Force.com and can take advantage of any and all new functionality without the hassle and risks of a traditional software upgrade process.
- There is no offline runtime environment for Force.com. You can edit your code on your desktop computer, but it must be sent to Force.com for execution.
- Apex is the only language that runs on the Force.com platform. You can integrate Apex with programs running outside of Force.com using HTTP-based techniques such as REST.
- The Force.com database is the only database integrated into the Apex language. Other databases can be integrated through Web services or other technology using HTTP.
The two primary choices for developing Apex code are the Web-based App Builder Tools and the Force.com IDE, provided as a stand-alone application as well as a plug-in to the standard Eclipse IDE. The Force.com IDE is the more powerful and developer-friendly of the two, so it is used throughout this book.