Visualforce in Practice

Visualforce in Practice

Michael Floyd Don Robins Matt Lacey Michael Topalovich Dan Appleman and Lior Gotersman Ryan Sieve Peter Gruhl

i

Copyrights

? Copyright 2000?2013 , inc. All rights reserved. is a registered trademark of , inc., as are other names and marks. Other marks appearing herein may be trademarks of their respective owners. Various trademarks held by their respective owners.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Written by Michael Floyd, Don Robins, Michael Topalovich, Dan Appleman and Lior Gotersman, Ryan Sieve and Peter Gruhl.

Contributions by Pat Patterson, Sandeep Bhanot, Raja Rao DV, and Michael Alderete.

Cover by Domenique Sillett.

Special Thanks to Nick Tran, Adam Seligman, Quinton Wall, Andrew Waite, Chris Wall, and Lesley Schneiderman.

ii

Introduction

The making of this book is an interesting story. The seed of its inspiration came from developers like you. Following one of our ELEVATE Workshops, an attendee was asked what he would like to see next. He simply replied "A book about Visualforce."

Then my colleague Mario Korf spoke up, saying that "We have an Advanced Apex book," authored by Dan Appleman to compliment our Introductory Apex Developer's Guide. "We should do an Advanced Visualforce." And that's how this book was born.

But rather than follow the Beginner-Advanced documentation model, I wanted a book that shows developers how to do real things while learning through actual apps that have been deployed and are in practice. This is Visualforce in Practice.

I also wanted readers to get the opportunity to learn from practitioners, developers in the trenches that are earnestly building enterprise applications and deploying them to the cloud. I wanted to share the experience of those who have gone before you.

And so I recruited Don Robins, a veteran Certified Salesforce Trainer and Salesforce MVP--the only developer to hold both titles. With Don's help we recruited Matt Lacey, Michael Topalovich, Dan Appleman, Lior Gotesman, Ryan Sieve and Peter Gruhl.

Together we've assembled a collection of Visualforce example apps that you can use in your own practice. This is not documentation, and it is not intended to document every feature built into Visualforce. This book is intended to share the experience and expertise of our authors.

About the Book

Visualforce in Practice is intended for intermediate developers. The book assumes you have walked through the Visualforce Workbook tutorial, and know the basics of creating a Visualforce page.

Chapter 1, Thinking in Visualforce lays the groundwork by discussing the MVC design Pattern, when you should use Visualforce and alternatives for using Visualforce under various use cases.

The Standard Controller is an amazingly versatile component that changes behavior based on the objects it's working on. In that sense it is a polymorphic component that can do an amazing amount of work without having to write code. In Chapter 2, Don Robins walks you through the Standard Controller and sheds light on mysterious components like URLFOR().

Because you'll need to write custom controllers or controller extensions at some point, Chapter 4 introduces you to "Just Enough Code." Rather than providing introductions to Apex and SOQL, this chapter gives you the basics so you can get started writing controllers and extensions immediately.

In Part II the rubber meets the road with chapters that will show you how to work with and paginate lists using the StandardSetController (Chapter 4), and how to create wizards that walk users through a process (Chapter 5). Chapter 6 shows how to create amazing looking charts

iii

to display data using the Analytics API, and Chapter 7 walks through the process for creating dashboards.

Chapter 8 uses the Streaming API to stream live data into Visualforce pages, while Chapter 9 shows how to build Visualforce page templates like those in the Salesforce Mobile Templates. Chapter 10 shows you how to add JavaScript and HTML5 to your Visualforce pages, which sets up Part III covering mobile development.

Chapter 11 walks you through the process of refactoring existing apps, then extends to mobile the Warehouse App used in the Workbook. Chapter 12 introduces you to the Salesforce Mobile Templates, the brainchild of Developer Evangelist Sandeep Bhanot. And Finally Chapter 13 presents readers with tips and tricks for optimizing Visualforce pages to ensure peek performance.

Looking back, there are many topics we would like to have covered in this book, and with the introduction of Salesforce1 there are many new topics we would like to include in the future. Indeed, I'm looking forward to creating a second edition of Visualforce in Pracice. In the meantime, it is my sincerest hope that you find this book not only educational, but useful in your everyday practice.

Sincerely,

Michael Floyd Editor in Chief, Developer Force

iv

Contents

Introduction................................................................................................................................... ii About the Book............................................................................................................................. iii

Chapter 1--Thinking in Visualforce...........................................................................9

What is Visualforce...................................................................................................................... 10 Controllers and Extensions......................................................................................................... 10 About the Markup....................................................................................................................... 11 About View State......................................................................................................................... 11 What is Contained in the View State?......................................................................................... 12 Examining the View State............................................................................................................ 12 View State in Action.................................................................................................................... 13 New Addition to Visualforce....................................................................................................... 16 Salesforce1 and Visualforce......................................................................................................... 16 Five Things you should master before getting startted with Visualforce................................... 16 Parting Words.............................................................................................................................. 18

Chapter 2--The Standard Controller--Pushing the Envelope........................ 19

What Is a Standard Controller?................................................................................................... 21 What Does a Standard Controller Do?....................................................................................... 21 CONTROLLING VIEWS............................................................................................................ 22 Materializing a User Interface..................................................................................................... 22 Standard User Interface Flow...................................................................................................... 23 CONTROLLING DATA.............................................................................................................. 24 Referencing a Single Record........................................................................................................ 24 Loading Field Values................................................................................................................... 25 Loading Field Values in Apex...................................................................................................... 26 Referencing a List of Records...................................................................................................... 27 CONTROLLING ACTIONS AND NAVIGATION................................................................... 28 Actions as Navigation.................................................................................................................. 28 Stateful Actions............................................................................................................................ 28 Stateful Action Mechanisms........................................................................................................ 29 Demo Custom sObject Actions................................................................................................... 31 Stateless Actions........................................................................................................................... 31 Stateless Action Mechanisms....................................................................................................... 33 $Action Global Variable.............................................................................................................. 33

1

URLFOR() Directing to a Visualforce page................................................................................ 34 URLFOR() and $Action Considerations.................................................................................... 35 Adding URL parameters on Actions with URLFOR()............................................................... 36 STANDARD CONTROLLERS IN APEX................................................................................... 37 Injecting an ApexPages.StandardController Instance................................................................ 37 Fetching the ID or record............................................................................................................ 37 Leveraging Standard Controller Actions in Apex....................................................................... 38 Concurrency Considerations...................................................................................................... 38 Action Method Navigation with ApexPages.PageReference...................................................... 39 CONTROLLING SECURITY..................................................................................................... 39 Security Context Variables - Standard vs. Custom..................................................................... 39 Summary...................................................................................................................................... 40

Chapter 3--Just Enough Code Introduction to Controllers and Extensions......................................................................................... 41

Overriding Built-in Functionality............................................................................................... 42 Creating New Functionality........................................................................................................ 42 Overriding Permissions............................................................................................................... 42 Basic Extension and Custom Controller Anatomy..................................................................... 42 Objective...................................................................................................................................... 42 Extension Class Solution............................................................................................................. 43 Custom Controller Solution........................................................................................................ 45 When and How To Use Standard, Custom, and Extension Controllers.................................... 47 The View State.............................................................................................................................. 48 The Transient Keyword................................................................................................................ 49 Inner Classes................................................................................................................................ 49 Collections of Objects.................................................................................................................. 50 Sharing Settings........................................................................................................................... 51 Unit Testing.................................................................................................................................. 51 Writing Unit Tests For Extensions ............................................................................................. 52 Writing Unit Tests For Custom Controllers ............................................................................... 53 Conclusion................................................................................................................................... 54

2

Chapter 4--The Standard List Controller.............................................................. 55

Displaying Standard Lists of Records.......................................................................................... 55 Customized List Displays............................................................................................................ 56 Adding Pagination....................................................................................................................... 57 Simple Filtering - Leveraging Standard List Views..................................................................... 59 Mass Updating of Records Using Visualforce............................................................................. 59 Advanced Filtering--Using Standard Set Controllers Inside Custom Controllers.................... 62 Providing Useful Feedback.......................................................................................................... 65

Chapter 5--Building Wizards With Visualforce.................................................. 67

Controller Extensions.................................................................................................................. 68 Navigating Between Pages Using a Controller............................................................................ 69 Moving Data Between The Pages And Controller...................................................................... 71 Adding Sprints To the Project..................................................................................................... 72 Adding Milestones....................................................................................................................... 75 Tidying Up................................................................................................................................... 78 Test Coverage............................................................................................................................... 79

Chapter 6--Building Charts With Visualforce..................................................... 81

APPROACH................................................................................................................................ 81 CONSIDERATIONS................................................................................................................... 82 WHERE TO USE CHARTS........................................................................................................ 82 Standalone VisualForce Pages..................................................................................................... 82 Embedded VisualForce Pages...................................................................................................... 82 BUILDING VISUALFORCE CHARTS...................................................................................... 83 Getting Chart Data...................................................................................................................... 83 Processing Chart Data................................................................................................................. 83 Controller Method....................................................................................................................... 84 JavaScript Function...................................................................................................................... 84 JavaScript Array........................................................................................................................... 84 Basic Chart Elements................................................................................................................... 84 Creating the Chart Container..................................................................................................... 85 Creating a VisualForce Area Chart............................................................................................. 92 Summary...................................................................................................................................... 99

3

Chapter 7--Building Dashboards With Visualforce......................................... 101

Considerations............................................................................................................................101 Use Cases for Visualforce Dasboard Components....................................................................102 Charts..........................................................................................................................................102 Real-Time Updating Charts.......................................................................................................103 Rotating Content........................................................................................................................103 Tailored Content.........................................................................................................................103 Interactive Content.....................................................................................................................103 Tables, Lists, and Grids...............................................................................................................103 Data Mashups.............................................................................................................................104 Embedded Web Content............................................................................................................104 JavaScript Widgets......................................................................................................................104 Building Visualforce Dashboard Components..........................................................................105 Adding Visualforce Pages to Dashboards..................................................................................105 Visualforce Page Component Configuration.............................................................................105 Component Container Size Limitations....................................................................................105 Dashboards in Practice...............................................................................................................106 Creating a Sales Leaderboard Dashboard Component.............................................................106 Creating the Controller..............................................................................................................106 Rolling Up Opportunity Data....................................................................................................107 Retrieving User Data..................................................................................................................108 Creating a Wrapper Class...........................................................................................................109 Creating the Visualforce Page.....................................................................................................110 Creating the Custom Visualforce Dashboard Component........................................................111 Summary.....................................................................................................................................114

Chapter 8--Streaming Realtime Data into Visualforce................................... 115

Streaming API Overview............................................................................................................116 Environment Setup and Configuration......................................................................................117 Create a Push Topic....................................................................................................................118 Visualforce Page with Client Side Processing............................................................................119 Visualforce Page with RemoteAction.........................................................................................124 Visualforce Page with ActionFunction......................................................................................128 Summary.....................................................................................................................................135

4

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download