You are on page 1of 1320

Force.

com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
.......Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6:Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data

Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web



are trademarks of salesforce.com, inc. All other trademarks mentioned in this
document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,



Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and
the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.
Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents
Chapter 3:Best Practices for Writing S-Controls.............................43
Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Force.com Cookbook

Copyright 2000-2007 salesforce.com, inc. All rights reserved. Salesforce.com and


the

no software

logo are registered trademarks, and AppExchange,

Success On Demand,

and

The Business Web

are trademarks of salesforce.com, inc. All other trademarks mentioned in this


document are theproperties of their respective owners.No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, in anyform, or by
any means, electronic, mechanical, photocopying, recording, or otherwise, without
theprior consent of the publisher.ISBN: 978-0-9789639-2-7 The authors would like to
thank additional contributors Dave Carroll, Simon Fell, Steve Fisher, ChrisFry,
Richard Greenwald, Andrea Leszek, Marcus Spohn, Nick Tran, Craig Weissman, and
SarahWhitlock. Additional thanks to Andrew Albert, Stefanie Andersen, Steve
Anderson, Gavin Austin,Eric Bezar, Manoj Cheenath, Leah Cutter, Michael
Dannenfeldt, Carolyn Dismuke, Bill Eidson, AdamGross, Michelle Jowitt, Paul
Kopacki, Loretta Macklem, Sarah Marovich, Reena Mathew, TaggartMatthiesen, Chris
McGuire, Yudi Nagata, Kavindra Patel, Igor Pesenson, Vahn Phan, Bhavana Rehani,EJ
Rice, Emad Salman, Mary Scotton, Jerry Sherman, Sagar Wanaselja, Jill Wetzler, and
Jim Yu fortheir advice and support.

Table of Contents
Introduction.......................................................................
.............1
Welcome to the Force.com
Platform....................................................................1Force.
com
Builder............................................................................
.........3S-
Controls...........................................................................
......................6 The Force.com Web Services
API............................................................7Apex...............
...................................................................................
........7Visualforce...............................................................
.................................8About This
Book...............................................................................
...................8Intended
Audience...........................................................................
.........8 The Sample Recruiting
App.....................................................................9Code
Samples............................................................................
.............10Sending
Feedback...........................................................................
........10
Chapter 1:Getting
Started..............................................................11
Signing Up for a Free Developer Edition
Account.............................................12Finding
Documentation......................................................................
...............13Finding Training
Courses............................................................................
.......14Deciding What to
Build..............................................................................
.......15Installing the Force.com Toolkit for
Eclipse.......................................................15Creating a Project
in
Eclipse............................................................................
...17Installing AJAX
Tools..............................................................................
...........18Installing the Force.com Explorer (for
Windows)..............................................19Installing SoqlXplorer (for
Mac OS X)..............................................................20Installing
the Flex Toolkit for
Force.com............................................................21
Chapter 2:Developing On-Demand Apps.......................................23
Targeting a Salesforce
Edition............................................................................
24Deciding When to Use S-Controls, Client Apps, Apex, or
Visualforce.............30Building and Deploying an
App.........................................................................33Creat
ing a Sandbox
Organization.......................................................................
35Migrating Metadata Between Two
Organizations..............................................38Migrating Apex Between
Two Salesforce Organizations....................................39
i
Table of Contents

Chapter 3:Best Practices for Writing S-Controls.............................43


Writing S-Controls in
Eclipse............................................................................
44 Writing S-Controls in AJAX
Tools....................................................................45Using
the AJAX Toolkit in an S-
Control...........................................................46Adding a Flex
Application to an S-
Control.......................................................49Using S-Control
Snippets in Other S-
Controls.................................................51Using Special Characters
in URLs and Custom Links.......................................52
Chapter 4: Best Practices for Writing Apex......................................55
Getting Started with
Apex...............................................................................
...56Choosing Between Classes or
Triggers...............................................................59 Writing
Apex in
Eclipse............................................................................
.........60Bulk Processing Records in a
Trigger..................................................................61Controll
ing Recursive
Triggers...........................................................................
62Comparing Queries Against Trigger.old and
Trigger.new..................................66De
f
ning an Apex Web Service and Calling It from an S-
Control.....................67Avoiding Apex Governor
Limits........................................................................70
Writing Unit Tests for
Apex...............................................................................
80Determining Apex Test
Coverage.......................................................................85
Writing Object-Oriented
Apex..........................................................................87
Chapter 5:Searching and Querying Data.........................................93
Using the Force.com Explorer to Examine Your Data
Model............................94Using SoqlXplorer to Examine Your Data
Model..............................................96Choosing Between SOQL and
SOSL................................................................99Constructing
SOQL and SOSL Queries in the Force.com Explorer...............100 Traversing Object
Relationships through the API............................................103Finding
a Contact, Lead, or Person
Account....................................................104Filtering Data Based
on a Relative
Date...........................................................106Finding Data
Based on
Division.......................................................................111P
reviewing Query
Results............................................................................
.....112Sorting Query
Results............................................................................
...........113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents
....113
Chapter 6: Improving Data Quality...............................................117
Preventing Duplicate Records from
Saving......................................................118Populating a Contact
Edit Page with Default Values from an Account............124Creating a Default
Interviewer Record When a Position is Created................125
ii
Table of Contents

You might also like