Apex Workbook PDF
Apex Workbook PDF
Copyright 20002014 salesforce.com, inc. All rights reserved. Salesforce.com is a registered trademark of salesforce.com,
inc., as are other names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
Apex Workbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Part 1: Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Creating Warehouse Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Using the Developer Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Activating the Developer Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Using the Developer Console to Execute Apex Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Creating Sample Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Creating and Instantiating Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Creating an Apex Class Using the Developer Console . . . . . . . . . . . . . . . . . . . . . . . . . 8
Calling a Class Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Creating an Apex Class Using the Salesforce User Interface . . . . . . . . . . . . . . . . . . . . . 10
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Part 2: Apex Language Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Primitive Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Boolean and Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Time, Date, and Datetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Integer, Long, Double and Decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Null Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Comments, Case Sensitivity, Collections and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Arrays and Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Sets and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Classes, Interfaces and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Defining Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Private Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Static Variables, Constants, and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Property Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
sObjects and the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
What is an sObject? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
SOQL and SOSL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Traversing and Querying sObject Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
SOQL For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Apex Data Manipulation Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
What Is an Exception? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Try, Catch, and Finally Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Built-In Exceptions and Common Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Catching Different Exception Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Creating Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Part 3: Apex in Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Executing Data Operations as a Single Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Adding Custom Business Logic Using Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Creating a Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Invoking the Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Apex Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Adding a Test Utility Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Adding Test Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Running Tests and Code Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Running Apex Within Governor Execution Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Scheduled Execution of Apex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Adding a Class that Implements the Schedulable Interface . . . . . . . . . . . . . . . . . . . . . 58
Adding a Test for the Schedulable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Scheduling and Monitoring Scheduled Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Apex Batch Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Adding a Batch Apex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Adding a Test for the Batch Apex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Running a Batch Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Apex REST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Adding a Class as a REST Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Creating a Record Using the Apex REST POST Method . . . . . . . . . . . . . . . . . . . . . . . . . 68
Retrieving a Record Using the Apex REST GET Method . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Visualforce Pages with Apex Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Enabling Visualforce Development Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Contents
Creating a Simple Visualforce Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Displaying Product Data in a Visualforce Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Using a Custom Apex Controller with a Visualforce Page . . . . . . . . . . . . . . . . . . . . . . 75
Using Inner Classes in an Apex Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Adding Action Methods to an Apex Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Contents
APEX WORKBOOK
Force.comApex is a strongly-typed, object-oriented programming language that allows you to write code that executes on the Force.com
platform. Out of the box, Force.com provides a lot of high-level services, such as Web services, scheduling of code execution, batch
processing, and triggersall of which require you to write Apex.
About the Apex Workbook
This workbook provides an introduction to both the Apex programming language, as well as the contexts in which you can use Apexsuch
as triggers.
This workbook does assume you know a little about programming. If you dont, youll still manage to follow along, but it will be a little
more difficult. We recommend Head First Java to start learning about programming. Although the book is about Java, Java is quite similar
to Apex in many ways, and it will provide the foundation you need.
The workbook is organized into three chapters:
Part 1: Orientation shows you the basics: how to create a simple Apex class, and how to use the Developer Console to execute
Apex snippets.
Part 2: Apex Language Fundamentals looks at the syntax, type system, and database integration found in the Apex language.
Part 3: Apex in Context looks at how to use Apex to write triggers, unit tests, scheduled Apex, batch Apex, REST Web services, and
Visualforce controllers.
The goal is to give you a tour of Apex, not build a working application. While touring along, feel free to experiment. Change the code a
little, substitute a different componenthave fun!
Intended Audience
This workbook is intended for developers new to the Force.com platform who want an introduction to Apex development on the
platform, and for Salesforce admins who want to delve more deeply into app development using coding. If youre an admin just getting
started with Force.com, see the Force.com Platform Fundamentals for an introduction to point-and-click app development.
Supported Browsers
Comments Browser
If you use Internet Explorer, we recommend using the latest version that Salesforce
supports. Apply all Microsoft software updates. Note these restrictions.
Microsoft
Internet Explorer
versions 7, 8, 9, 10,
and 11
The full Salesforce site is not supported in Internet Explorer on touch-enabled
devices for Windows. Use the Salesforce1 mobile browser app instead.
The HTML solution editor in Internet Explorer 11 is not supported in Salesforce
Knowledge.
The Compatibility View feature in Internet Explorer isnt supported.
The Metro version of Internet Explorer 10 isnt supported.
1
Comments Browser
Internet Explorer 6 and 7 arent supported for login hints for multiple accounts.
Internet Explorer 7 and 8 arent supported for the Developer Console or the
Data Import Wizard.
Internet Explorer 7 isnt supported for Open CTI.
Internet Explorer 7 and 11 arent supported for Salesforce CRM Call Center built
with CTI Toolkit version 4.0 or higher.
Internet Explorer 7 isnt supported for Force.com Canvas.
Internet Explorer 7 isnt supported for Salesforce console features that require
more advanced browser performance and recent Web technologies. The console
features not available in Internet Explorer 7 include:
The Most Recent Tabs component
Multiple custom console components on sidebars
Vertical auto-sizing for stacked console components in sidebars
Font and font color for console components Button CSS
Multi-monitor components
The resizable highlights panel
The full-width feed option on feed-based page layouts
Internet Explorer 7 and 8 arent supported for Community Templates for
Self-Service.
Internet Explorer 7 and 8 have performance issues when Multi-Line Edit in
Opportunity Splits is used.
Community Templates for Self-Service supports Internet Explorer 9 and above
for desktop users and Internet Explorer 11 and above for mobile users.
For configuration recommendations, see Configuring Internet Explorer in the
Salesforce Help.
Salesforce.com makes every effort to test and support the most recent version of
Firefox.
Mozilla
Firefox
Safari