Visualforce in Practice

  • View
    12

  • Download
    1

Embed Size (px)

DESCRIPTION

Workbook for learning visual force

Text of Visualforce in Practice

  • iPB

    Visualforce in Practice

    Michael FloydDon RobinsMatt LaceyMichael TopalovichDan Appleman and Lior GotersmanRyan Sieve Peter Gruhl

  • iiiii

    Copyrights Copyright 20002013 salesforce.com, inc. All rights reserved. Salesforce.com is a registered trademark of salesforce.com, inc., as are other names and marks. Other marks appearing herein may be trademarks of their respective owners. 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.

  • iiiii

    IntroductionThe 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 Developers Guide. We should do an Advanced Visualforce. And thats 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 Force.com 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 Force.com Certified Salesforce Trainer and Salesforce MVPthe only developer to hold both titles. With Dons help we recruited Matt Lacey, Michael Topalovich, Dan Appleman, Lior Gotesman, Ryan Sieve and Peter Gruhl.

    Together weve 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 BookVisualforce in Practice is intended for intermediate Force.com 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 its 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 youll 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

  • 1iv

    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 Force.com apps, then extends to mobile the Warehouse App used in the Force.com 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, Im 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 FloydEditor in Chief, Developer ForceSalesforce.com

  • 1iv

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

    Chapter 1Thinking 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 2The Standard ControllerPushing 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 ..... . . . . . . . . . . . . . . . . . . . . . . . .