Reader small image

You're reading from  Visualforce Development Cookbook

Product typeBook
Published inSep 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781782170808
Edition1st Edition
Languages
Right arrow
Author (1)
Keir Bowden
Keir Bowden
author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden

Right arrow

Chapter 5. Managing Multiple Records

In this chapter, we will cover the following recipes:

  • Preventing duplicates by searching before creating

  • Editing a record and its parent

  • Managing a list of records

  • Converting a lead

  • Managing a hierarchy of records

  • Inline-editing a record from a list

  • Creating a Visualforce report

  • Loading records asynchronously

Introduction


When Visualforce pages utilize a controller extension or custom controller, they can retrieve additional records via SOQL queries. This allows pages to manage more than one record, regardless of the record sObject types or whether there is any relationship between the records.

Note

The Salesforce Object Query Language (SOQL) allows information to be retrieved from the Salesforce database based on supplied criteria. It has an SQL-like syntax, but does not support advanced operations such as wildcard field lists.

In this chapter, we will explore a number of scenarios to manage multiple records on a single page, ranging from a single record and its parent to a deep and wide hierarchy.

We will also see how Visualforce can be used to present details of a collection of records in response to user-specified criteria, in order to search for existing matches before creating a new record or to produce a custom report page.

Preventing duplicates by searching before creating


A very common problem in Salesforce implementations is duplicate data. While training users to search for existing records before creating can help, this relies on users remembering to follow the process, which can be especially problematic if some users create records on an infrequent basis.

In this recipe, we will create a Visualforce page that overrides the lead sObject create button and requires a user to search for existing matching records before they are allowed to create a new record. In order to avoid the user having to rekey data in the event that no matches are found, the search criteria is carried through to the create page.

Getting ready

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

Note

This page does not make use of the standard controller, but the page is required to use the lead standard controller in order to be able to override a standard button.

How to do it…

  1. Navigate to...

Editing a record and its parent


A Visualforce page managed by a standard controller can provide the edit capability for a record and its parent. However, when the standard controller Save method is invoked, the object graph is not traversed and only the record being managed by the controller is saved.

In this recipe we will create a Visualforce page to allow a user to edit fields from a contact and its parent account. Saving the record will also apply any changes made to the parent account record.

Getting ready

This recipe makes use of an extension 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 ContactAndAccountEditExt.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create the Visualforce page by navigating to the Visualforce...

Managing a list of records


Salesforce users often require the capability to work with a number of records at once. For example, a sales user may be communicating with a number of contacts, while he/she may also be creating or deleting contacts in response to information received through a number of channels. The Salesforce enhanced list view functionality allows a set of records that share a common record type to be inline-edited, but doesn't provide a way to add or remove records.

In this recipe, we will create a Visualforce page to allow a user to edit the details of a collection of existing contact records, and create or delete records dynamically. Upon saving the list, existing records will be updated, any new records will be inserted, and any records previously deleted from the collection will also be deleted from the database.

Getting ready

This recipe makes use of a wrapper class that needs to be created before the Visualforce page.

  1. Navigate to the Apex Classes setup page by clicking...

Converting a lead


The standard Salesforce lead conversion page allows a user to create a new account and contact record or merge with existing records, and optionally create a new opportunity record. Information from the lead is copied to the existing or new records based on the lead field mapping configuration. If a user wishes to specify information after clicking on the Convert button, they must exit the conversion and edit the lead record.

In this recipe, we will create a Visualforce page to allow a user to convert a lead and in addition to creating or merging with existing records, populate additional fields on the opportunity that is created as part of the conversion.

Getting ready

This recipe makes use of a controller extension 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 LeadConvertExt.cls Apex class from...

Managing a hierarchy of records


Salesforce records often form part of a deep and wide hierarchy; for example, an account can contain a number of cases, each of which can have a number of comments associated with them. Creating and maintaining the elements of the hierarchy in isolation is a cumbersome and time-consuming task, as to add a comment a user must click through from the account record to the case record, and then click on the New button on the case comments related list to open the new comment page.

In this recipe we will create a Visualforce page that allows a user to maintain an account, its associated cases, and the comments associated with those cases. The user can update or delete existing records, or create new records at any level of the hierarchy.

Getting ready

This recipe makes use of two wrapper classes which need to be created before the Visualforce page and controller.

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click...

Inline-editing a record from a list


In the previous recipes, all records in a list have been editable. This works well when the user is expecting to edit a number of records, but is not a great experience for users that predominantly view records. In this situation, it is better to give the user a read-only view of the records and a rapid way to edit a record if required.

In this recipe, we will create a Visualforce page that presents a list of contacts in read-only mode. The user may double-click on any field in order to edit the record details. Any changes to the contact records are stored locally, until the user chooses to save or discard them.

Note

Standard Visualforce markup provides support for inline editing, but this requires each field to be double-clicked to edit individually.

Getting ready

This recipe makes use of a custom controller that 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...

Creating a Visualforce report


Salesforce provides powerful analytic capabilities through the report and dashboard builders, but there are times when reporting requirements cannot be satisfied through the standard functionality; for example, where data from a number of different sources is required to be presented in multiple formats. In this scenario, Visualforce can give fine-grained control over the layout of the results, while a custom controller allows retrieval of any accessible data in the system.

In this recipe, we will create a Visualforce report that retrieves all cases matching criteria specified by the user and outputs these in a tabular format containing details of all cases, keeping a running total of the number of cases with the same status and origin. Two tables that provide the total count of cases for each status and origin value follow this.

Note

Note that the replacement of standard reporting functionality with Visualforce should only be carried out as a last resort. Coding...

Loading records asynchronously


In the previous recipes, all lists of records being managed by the page or related to the record being managed have been loaded synchronously; that is, the records have been retrieved by the controller and displayed when the page is initially loaded. In the event that the query retrieving the records is complex (and thus, time consuming), or where the payload for the records is large due to the volume of records or the size of each individual record, this can result in a delay before the page is loaded. A delay of this nature is invariably a negative experience for the user, often leading them to conclude that the application has failed in some way.

In this recipe, we will create a Visualforce page that loads an account record prior to rendering the page for the first time, and then loads the opportunity records associated with the account asynchronously. A spinning GIF is displayed to the user indicating that the asynchronous load is taking place.

Getting ready...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013Publisher: PacktISBN-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.
undefined
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

Author (1)

author image
Keir Bowden

Keir Bowden is a 30-year veteran of the IT industry from the United Kingdom. After spending the early part of his career in the defence industry, he moved into investment banking systems, implementing systems for Banque Nationale de Paris, CitiGroup, and Deutsche Bank. In the late 1990s, Keir moved into Internet technologies, leading to a development of the order management and payment handling systems of one of the first European Internet shopping sites. Keir started working with Force.com in late 2008 and has been recognized multiple times by Salesforce as an MVP for his contribution and leadership in the community. In 2012, he became the first certified technical architect outside of Salesforce in EMEA, and he has served as a judge on several EMEA Technical Architect Certification Review Boards. Keir is also a prominent blogger on Apex, Visualforce and Lightning Components solutions; and a regular speaker at events such as Dreamforce, Cloud World Forum, and Salesforce World Tour. Keir is a chief technical officer of BrightGena—a Salesforce.com Platinum Cloud Alliance Partner in the United Kingdom, where he is responsible for the present and future technical strategies. Keir acted as a technical reviewer for the CRM Admin Cookbook before accepting the challenge of authoring this book, which also happens to be his first.
Read more about Keir Bowden