Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Visualforce Development Cookbook

You're reading from  Visualforce Development Cookbook

Product type Book
Published in Sep 2013
Publisher Packt
ISBN-13 9781782170808
Pages 334 pages
Edition 1st Edition
Languages
Author (1):
Keir Bowden Keir Bowden
Profile icon Keir Bowden

Table of Contents (16) Chapters

Visualforce Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. General Utilities 2. Custom Components 3. Capturing Data Using Forms 4. Managing Records 5. Managing Multiple Records 6. Visualforce Charts 7. JavaScript 8. Force.com Sites 9. jQuery Mobile Index

Chapter 4. Managing Records

In this chapter, we will cover the following recipes:

  • Styling fields as required

  • Styling table columns as required

  • Attaching an image to a record

  • Managing attachments

  • Maintaining custom settings

  • Refreshing record details from embedded Visualforce

  • Using wrapper classes

  • Changing options based on the user input

  • Changing page layout based on the user input

  • Form-based searching

Introduction


One of the common use cases for Visualforce pages is to simplify, streamline, or enhance the management of sObject records. In the earlier chapters we have covered how Visualforce can be used to provide a custom or user interface to create and edit records.

In this chapter, we will use Visualforce to carry out some more advanced customization of the user interface—redrawing the form to change available picklist options, or capturing different information based on the user's selections. We will also see how Visualforce can be used to manage non-sObject information by providing custom user interfaces to allow custom settings and attachments to be maintained, and searching for records based on the value of specific fields.

Styling fields as required


Standard Visualforce input components, such as <apex:inputText />, can take an optional required attribute. If set to true, the component will be decorated with a red bar to indicate that it is required, and form submission will fail if a value has not been supplied as shown in the following screenshot:

In the scenario where one or more inputs are required and there are additional validation rules, for example, when one of either the Email or Phone fields is defined for a contact, this can lead to a drip feed of error messages to the user. This is because the inputs make repeated unsuccessful attempts to submit the form, each time getting slightly further in the process.

In this recipe we will create a Visualforce page that allows a user to create a contact record. The Last Name field is captured through a nonrequired input decorated with a red bar identical to that created for required inputs. When the user submits the form, the controller validates that the...

Styling table columns as required


When maintaining records that have required fields through a table, using regular input fields can end up with an unsightly collection of red bars striped across the table.

In this recipe we will create a Visualforce page to allow a user to create a number of contact records via a table. The contact Last Name column header will be marked as required, rather than the individual inputs.

Getting ready

This recipe makes use of a custom controller, so this will need to be created before the Visualforce page.

How to do it…

  1. First, create the custom controller by navigating 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 RequiredColumnController.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.

  6. Click on the New button...

Attaching an image to a record


Associating an image with a record is a common requirement while implementing Salesforce, for example, adding a photo to a contact or a custom news story sObject. Using the standard attachments functionality creates a disconnect between the record and the image, requiring additional clicks to view the image, and often relies on the user following a naming convention when uploading the file.

In this recipe, we will create a Visualforce page to allow a user to attach an image to a contact record. The page also displays the image if one has been uploaded. This page will be embedded into the standard contact page layout.

Note

While the size limit for a record attachment in Salesforce is 5 MB, as the attachment in this recipe is rendered on a Visualforce page, it is important to keep the size of the file as small as possible to avoid lengthy download times and excessive bandwidth usage.

Getting ready

This recipe makes use of a controller extension, so this will need...

Managing attachments


The standard mechanism of attaching files to Salesforce records navigates the user away from the record to a dedicated upload page. This leaves the user unable to see if they are duplicating an existing attachment, or see the exact details of any fields that may be required to name the attachment correctly.

In this recipe, we will create a Visualforce page to allow a user to attach files directly to a contact record, displaying fields from the record and details of any existing attachments.

Getting ready

This recipe makes use of a custom controller, so this must be present before the Visualforce page can be created.

How to do it…

  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 AttachmentsExt.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create Visualforce page by navigating to the Visualforce setup page by clicking on Your Name...

Maintaining custom settings


Custom settings are a natural fit for data that controls application behavior. They are similar to custom sObjects but are cached, and so do not have to be retrieved from the Salesforce database each time they are accessed. For more information, refer to the Custom Settings Overview page in the Salesforce online help. Unlike custom sObjects, custom settings do not have a configurable user interface provided by the platform, which can make maintenance a challenge for inexperienced administrators.

In this recipe, we will create a Visualforce frontend to an existing custom setting that allows an administrator to take an application in and out of maintenance, with an associated message to display to users.

Getting ready

This recipe makes use of a custom setting, so this will need to be created and populated before the Visualforce page can be created.

  1. Navigate to the Custom Settings setup page by clicking on Your Name | Setup | Develop | Custom Settings.

  2. Click on the New...

Refreshing record details from embedded Visualforce


A Visualforce page can be embedded into a standard or custom sObject record view page, providing the standard controller for the sObject type manages it. This technique is often used to allow information to be added to a record or its related lists without leaving the view page. The Visualforce page is embedded in the record view page using an iframe. This means that returning a page reference to send the user to the record view page after an update results in the entire record view page being displayed inside the iframe, as shown in the following screenshot:

In this recipe, we will create a Visualforce page that is embedded inside the standard case sObject record view page and allows a case comment to be added directly from the page. Upon saving a case comment, the entire record view page will be refreshed to display the updated case comments related list.

Getting ready

This recipe makes use of a controller extension, so this must be present...

Using wrapper classes


A common use case for Visualforce is to present a list of sObjects, allowing a user to select a number of these, and then choose an action to apply to the selected entries. Marking an sObject entry as selected presents a challenge, as it is associating transient information, the selected status, with a record persisted in the Salesforce database.

The solution is to use a wrapper class to encapsulate or wrap an sObject instance and some additional information associated with the sObject instance.

In this recipe, we will create a Visualforce page that presents a list of opportunity sObjects, and allows the user to select a number of records to remove from the displayed list.

Getting ready

This recipe makes use of a wrapper class which associates a checkbox with an opportunity sObject record.

  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 SelectOpportunityWrapper.cls Apex class...

Changing options based on the user input


In the earlier recipes, for example, the Retrieving fields when a lookup is populated recipe in Chapter 3, Capturing Data Using Forms, we have seen how to populate the contents of fields in response to user actions. This technique can also be used to change the characteristics of other input fields on the page in response to user selections.

In this recipe we will create a Visualforce page that allows a user to create five task sObject records at once. Each task is associated with a contact selected from a picklist. The picklist options are configured through a series of checkboxes; clearing a checkbox removes the contact from the picklist.

Getting ready

This recipe makes use of a custom controller, so this will need to be present before the Visualforce page can be created.

How to do it…

  1. First, create the custom controller for the Visualforce page by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click...

Changing page layout based on the user input


When a Visualforce form submission component specifies a rerender attribute, this causes a partial refresh of the page to take place, redrawing the specified components based on the result of the submission. This can be used to change the elements on the page based on the user input; for example, to guide the user through creating a record a few fields at a time.

In this recipe, we will create a Visualforce page that allows the user to create an account record. If the user chooses an account type containing the word customer, additional fields will be rendered to capture additional customer-specific information.

Getting ready

This recipe makes use of a controller extension, so this needs to be present before the Visualforce page can be created.

How to do it…

  1. First, create the custom controller for the Visualforce page by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste...

Form-based searching


Standard Salesforce searching looks for any occurrence of a supplied text value in all searchable fields of one or more sObject types. In the scenario where a user is interested in the occurrence of the text value in a particular field, this can lead to a number of unwanted results. For example, searching for an account whose name contains the text United will also retrieve all accounts with a mailing or billing address in the United Kingdom.

Form-based searching allows a user to specify the text that should be present in particular fields in order to be considered a match.

In this recipe, we will create a Visualforce page to allow a user to search for accounts that contain specified text in the Account Name or Website fields, or where the name entered in the Industry field matches one of a number of options.

Getting ready

This recipe makes use of a custom controller, so this will need to be created before the Visualforce page.

How to do it…

  1. First, create the custom controller...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013 Publisher: Packt ISBN-13: 9781782170808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}