Beruflich Dokumente
Kultur Dokumente
Apex Workbook
Last updated: May 14, 2013
Copyright 20002013 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.
Table of Contents
Apex Workbook...................................................................................................................................1
Chapter 1: Orientation.........................................................................................................................4
Tutorial #1: Creating Warehouse Custom Objects...................................................................................................................4
Tutorial #2: Using the Developer Console................................................................................................................................5
Lesson 1: Activating the Developer Console.................................................................................................................5
Lesson 2: Using the Developer Console to Execute Apex Code...................................................................................5
Summary.......................................................................................................................................................................8
Tutorial #3: Creating Sample Data...........................................................................................................................................8
Tutorial #4: Creating and Instantiating Classes........................................................................................................................8
Lesson 1: Creating an Apex Class Using the Developer Console.................................................................................8
Lesson 2: Calling a Class Method...............................................................................................................................10
Lesson 3: Creating an Apex Class Using the Salesforce User Interface.......................................................................11
Summary......................................................................................................................................................................12
Chapter 2: Apex Language Fundamentals............................................................................................13
Tutorial #5: Primitive Data Types and Variables....................................................................................................................13
Lesson 1: String...........................................................................................................................................................14
Lesson 2: Boolean and Conditional Statements..........................................................................................................15
Lesson 3: Time, Date, and Datetime..........................................................................................................................17
Lesson 4: Integer, Long, Double and Decimal............................................................................................................17
Lesson 5: Null Variables..............................................................................................................................................18
Lesson 6: Enums.........................................................................................................................................................19
Summary......................................................................................................................................................................19
Tutorial #6: Comments, Case Sensitivity, Collections and Loops..........................................................................................20
Lesson 1: Comments...................................................................................................................................................20
Lesson 2: Case Sensitivity............................................................................................................................................20
Lesson 3: Arrays and Lists...........................................................................................................................................20
Lesson 4: Loops...........................................................................................................................................................22
Lesson 5: Sets and Maps.............................................................................................................................................23
Summary......................................................................................................................................................................24
Tutorial #7: Classes, Interfaces and Properties........................................................................................................................24
Lesson 1: Defining Classes..........................................................................................................................................25
Lesson 2: Private Modifiers.........................................................................................................................................25
Lesson 3: Constructors................................................................................................................................................26
Lesson 4: Static Variables, Constants, and Methods...................................................................................................27
Lesson 5: Interfaces.....................................................................................................................................................28
Lesson 6: Property Syntax...........................................................................................................................................29
Summary......................................................................................................................................................................30
Tutorial #8: sObjects and the Database...................................................................................................................................31
Lesson 1: What is an sObject?.....................................................................................................................................31
i
Table of Contents
Lesson 2: SOQL and SOSL Queries..........................................................................................................................32
Lesson 3: Traversing and Querying sObject Relationships.........................................................................................33
Lesson 4: SOQL For Loops........................................................................................................................................34
Lesson 5: Apex Data Manipulation Language............................................................................................................34
Summary......................................................................................................................................................................37
Tutorial #9: Exception Handling............................................................................................................................................37
Lesson 1: What Is an Exception?................................................................................................................................37
Lesson 2: Try, Catch, and Finally Statements.............................................................................................................38
Lesson 3: Built-In Exceptions and Common Methods...............................................................................................40
Lesson 4: Catching Different Exception Types...........................................................................................................44
Lesson 5: Creating Custom Exceptions......................................................................................................................45
Summary......................................................................................................................................................................47
Chapter 3: Apex in Context.................................................................................................................48
Tutorial #10: Executing Data Operations as a Single Transaction.........................................................................................48
Tutorial #11: Adding Custom Business Logic Using Triggers...............................................................................................50
Lesson 1: Creating a Trigger.......................................................................................................................................50
Lesson 2: Invoking the Trigger...................................................................................................................................51
Summary......................................................................................................................................................................52
Tutorial #12: Apex Unit Tests................................................................................................................................................52
Lesson 1: Adding a Test Utility Class.........................................................................................................................53
Lesson 2: Adding Test Methods.................................................................................................................................54
Lesson 3: Running Tests and Code Coverage.............................................................................................................56
Summary......................................................................................................................................................................57
Tutorial #13: Running Apex Within Governor Execution Limits..........................................................................................58
Tutorial #14: Scheduled Execution of Apex............................................................................................................................60
Lesson 1: Adding a Class that Implements the Schedulable Interface........................................................................60
Lesson 2: Adding a Test for the Schedulable Class.....................................................................................................61
Lesson 3: Scheduling and Monitoring Scheduled Jobs................................................................................................62
Summary......................................................................................................................................................................63
Tutorial #15: Apex Batch Processing......................................................................................................................................63
Lesson 1: Adding a Batch Apex Class.........................................................................................................................64
Lesson 2: Adding a Test for the Batch Apex Class.....................................................................................................65
Lesson 3: Running a Batch Job...................................................................................................................................67
Summary......................................................................................................................................................................68
Tutorial #16: Apex REST.......................................................................................................................................................68
Lesson 1: Adding a Class as a REST Resource...........................................................................................................69
Lesson 2: Creating a Record Using the Apex REST POST Method.........................................................................70
Lesson 3: Retrieving a Record Using the Apex REST GET Method........................................................................71
Summary......................................................................................................................................................................72
Tutorial #17: Visualforce Pages with Apex Controllers..........................................................................................................72
Lesson 1: Enabling Visualforce Development Mode..................................................................................................72
Lesson 2: Creating a Simple Visualforce Page.............................................................................................................72
Lesson 3: Displaying Product Data in a Visualforce Page...........................................................................................73
Lesson 4: Using a Custom Apex Controller with a Visualforce Page..........................................................................76
ii
Table of Contents
Lesson 5: Using Inner Classes in an Apex Controller.................................................................................................78
Lesson 6: Adding Action Methods to an Apex Controller.........................................................................................80
Summary......................................................................................................................................................................82
iii
Table of Contents
iv
Table of Contents
Apex Workbook
Force.com Apex 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:
Chapter 1: Orientation shows you the basics: how to create a simple Apex class, and how to use the Developer Console
to execute Apex snippets.
Chapter 2: Apex Language Fundamentals looks at the syntax, type system, and database integration found in the Apex
language.
Chapter 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. Apply
all Microsoft software updates. Note these restrictions.
Microsoft
Internet Explorer
versions 7, 8,
9, and 10
The Compatibility View feature in Internet Explorer isnt supported.
The Metro version of Internet Explorer 10 isnt supported.
Internet Explorer 7 and 8 arent supported for the Developer Console.
Internet Explorer 10 isnt supported for the Service Cloud console.
Internet Explorer 7 isnt supported for Open CTI.
Internet Explorer 7 isnt supported for Salesforce CRM Call Center built
with CTI Toolkit version 4.0 or higher.
Internet Explorer 7 isnt supported for Force.com Canvas.
For configuration recommendations, see Configuring Internet Explorer in
the online help.
1
Apex Workbook
Comments Browser
Salesforce.com makes every effort to test and support the most recent version
of Firefox. For configuration recommendations, see Configuring Firefox in
the online help.
Mozilla
Firefox
plug-in for
Microsoft
Internet Explorer
6 and 7
For configuration recommendations, see Installing the Google Chrome
Frame
Internet Explorer
Safari