Book Image

Visualforce Development Cookbook - Second Edition

By : Keir Bowden
Book Image

Visualforce Development Cookbook - Second Edition

By: Keir Bowden

Overview of this book

Visualforce is a framework that allows developers to build sophisticated, custom user interfaces that can be hosted natively on the Force.com platform. The Visualforce framework includes a tag-based markup language, similar to HTML that is used to write the Visualforce pages and a set of controllers that are used to write business logic to the Visualforce pages. Visualforce Development Cookbook provides solutions to a variety of challenges faced by Salesforce developers and demonstrates how easy it is to build rich, interactive pages using Visualforce. Whether you are looking to make a minor addition to the standard page functionality or override it completely, this book will provide you with the help you require throughout. You will start by learning about the simple utilities and will build up to more advanced techniques for data visualization and to reuse functionality. You will learn how to perform various tasks such as creating multiple records from a single page, visualizing data as charts, using JavaScript to enhance client-side functionality, building a public website, and making data available to a mobile device. With an interesting chapter on tackling common issues faced while developing Visualforce pages, the book provides lots of practical examples to enhance and extend your Salesforce user interface.
Table of Contents (16 chapters)
Visualforce Development Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Opening a pop-up window


Pop-up browser windows have received mixed reviews in recent years. Originally created, before tabbed browsers existed, to display additional information without interfering with the page the user had navigated to, they were quickly hijacked and used to display advertisements and spam. Pop-ups should be used sparingly in applications and wherever possible in response to an action by the user.

The target attribute can be specified as _blank on HTML hyperlink tags to open the link in a new window, but all modern browsers allow the user to specify that new windows should be opened as new tabs instead. Also, if the browser does open the URL in a new window, it will be of the same size as the existing window and block most of it. Opening a window in JavaScript allows for fine-grained control over many aspects of the pop-up window, for example, the size and whether to display a toolbar.

In this recipe, we will create a page that renders a list of accounts, displaying a very small subset of fields per row. A link will be provided on each row to allow the user to view full details of the account in a pop-up window.

Note

Note that there is no way to ensure that a browser will display a pop-up window. Pop-up blockers generally allow windows to be opened in response to an action by the user, such as clicking on a link, but it is possible for users to configure their browser to block all pop-ups regardless of how they were triggered.

How to do it...

This recipe requires two Visualforce pages to be created: the main page containing the list of accounts and the pop-up window page. The pop-up page is referenced by the main page, so this will be created first:

  1. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Visualforce Pages.

  2. Click on the New button.

  3. Enter Popup in the Label field.

  4. Accept the default Popup that is automatically generated for the Name field.

  5. Paste the contents of the Popup.page file from the code download into the Visualforce Markup area.

  6. Click on the Save button to save the page.

  7. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Visualforce Pages.

  8. Locate the entry for the Setup page and click on the Security link.

  9. On the resulting page, select which profiles should have access and click on the Save button.

  10. Next, create the main account list page by navigating to the Visualforce setup page, clicking on Your Name | Setup | Develop | Visualforce Pages.

  11. Click on the New button.

  12. Enter PopupMain in the Label field.

  13. Accept the default PopupMain that is automatically generated for the Name field.

  14. Paste the contents of the PopupMain.page file from the code download into the Visualforce Markup area.

  15. Click on the Save button to save the page.

  16. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Visualforce Pages.

  17. Locate the entry for the PopupMain page and click on the Security link.

  18. On the resulting page, select which profiles should have access and click on the Save button.

How it works...

Opening the following URL in your browser displays a list of accounts: https://<instance>/apex/PopupMain.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

Note

Note that, as this page uses a standard list controller, the list of accounts displayed will be that of the last list view that the user accessed.

The detail link markup is as follows:

<apex:outputLink   
    onclick="return openPopup('{!acc.Id}');"> 
    Details 
</apex:outputLink> 

The onclick attribute defines the JavaScript function to be invoked when the link is clicked; note the {!acc.id}merge field, which passes the ID of the chosen account to the function.

The JavaScript function uses the window.open function to open the new window:

var newWin=window.open   ('{!$Page.Popup}?id=' + id, 'Popup',          '   height=600,width=650,left=100,top=100,resizable=no,   scrollbars=yes,toolbar=no,status=no');

The final parameter details the features required for the new window as a comma-separated list of name=value pairs.

Clicking on the Details link displays the full account details in a pop-up window:

See also

  • The Adding a custom lookup to a form recipe in Chapter 3, Capturing Data Using Forms shows how information can be captured in a pop-up window and passed back to the main window to populate input fields.