Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Visualforce Development Cookbook
  • Table Of Contents Toc
Visualforce Development Cookbook

Visualforce Development Cookbook - Second Edition

By : Keir Bowden
4.9 (8)
close
close
Visualforce Development Cookbook

Visualforce Development Cookbook

4.9 (8)
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 (11 chapters)
close
close

Passing parameters between Visualforce pages

If a user is redirected from one Visualforce page to another and they both share the same controller and extensions, the controller instance will be retained and re-used, allowing the second page to access any information captured by the first.

If the pages do not share the same controller and extensions, the controller instance will be discarded and the second page will have no access to any information captured by the first. If the state needs to be maintained across the pages in this case, it must be encapsulated in the parameters on the URL of the second page.

In this recipe, we will build on the example from the previous recipe to create a Visualforce search page to retrieve all accounts where the name contains a string entered by the user, and provide a way for the user to edit selected fields on all the accounts returned by the search. The record IDs of the accounts to edit will be passed as parameters on the URL to the edit page.

How to do it...

As the search page makes reference to the edit page, the edit page and associated custom controller must be created first:

  1. Navigate to the Apex Classes setup page by clicking on Your Name|Setup|Develop|Apex Classes.
  2. Click on the New button.
  3. Paste the contents of the EditFromSearchController.cls Apex class from the code download into the Apex Class area.
  4. Click on the Save button.
  5. Next, create the edit Visualforce page by navigating to the Visualforce setup page, clicking on Your Name|Setup|Develop|Visualforce Pages.
  6. Click on the New button.
  7. Enter EditFromSearch in the Label field.
  8. Accept the default EditFromSearch that is automatically generated for the Name field.
  9. Paste the contents of the EditFromSearch.page file from the code download into the Visualforce Markup area.
  10. Click on the Save button to save the page.
  11. Navigate to the Visualforce setup page by clicking on Your Name|Setup|Develop|Visualforce Pages.
  12. Locate the entry for the EditFromSearch page and click on the Security link.
  13. On the resulting page, select which profiles should have access and click the Save button.
  14. Next, create the search page by navigating to the Apex Classes setup page, clicking on Your Name|Setup|Develop|Apex Classes.
  15. Click on the New button.
  16. Paste the contents of the SearchAndEditController.cls Apex class from the code download into the Apex Class area.
  17. Click on the Save button.
  18. Next, create the Visualforce page by navigating to the Visualforce setup page, clicking on Your Name|Setup|Develop|Visualforce Pages.
  19. Click on the New button.
  20. Enter SearchAndEdit in the Label field.
  21. Accept the default SearchAndEdit that is automatically generated for the Name field.
  22. Paste the contents of the SearchAndEdit.page page from the code download into the Visualforce Markup area.
  23. Click on the Save button to save the page.
  24. Navigate to the Visualforce setup page by clicking on Your Name|Setup | Develop|Visualforce Pages.
  25. Locate the entry for the SearchAndEdit page and click on the Security link.
  26. 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 retrieves all accounts where the name field contains the string United :  https://<instance>/apex/SearchAndEdit?name=United.

How it works...

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

Notice that the page contains an Edit button, and clicking on this executes the following action method:

public PageReference edit() 
{ 
  PageReference pr=Page.EditFromSearch; 
  Integer idx=1; 
  for (Account acc : accounts) 
  { 
    pr.getParameters().put('account' + idx, acc.id); 
    idx++; 
  } 
     
  return pr; 
} 

This method initially creates a page reference for the edit page-EditFromSearch. It then iterates the accounts in the search results and adds an entry to the page reference parameters for the account ID. Each parameter has the name account, concatenated with the index of the result, starting from 1. This will result in a URL of the form https://<instance>/apex/EditFromSearch?account1=001i0000006OVLIAA4&account2=001i0000006OVLJAA4.

The EditFromSearch page then renders a form with an editable row per account:

How it works...

The constructor of EditFromSearchController that manages the data for the page extracts the IDs from the URL and adds them to a list, starting with account1, until it hits a parameter index that is not present in the URL:

Integer idx=1; 
String accStr; 
do  
{ 
  accStr=ApexPages.currentPage().getParameters(). 
get('account' + idx); 
  if (accStr!=null) 
  { 
    ids.add(accStr); 
  } 
  idx++; 
} 
while (null!=accStr); 

The action method that saves the user's edits redirects them to the standard account tab once the save is complete:

return new PageReference('/001/o'); 

Note

Note that accessing the standard tab via this URL is not supported by Salesforce, and if the URL scheme or three-character prefix for account (001) were to change, this redirection would stop working.

See also

  • The Reacting to URL parameters recipe in this chapter shows how a controller can process URL parameters prior to rendering a Visualforce page.
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Visualforce Development Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon