Visualforce Example - Mass update records

One of the greatest examples of the implementation of Visualforce will be a mass update of records. In this example, we will create pages that can perform mass updates. It will use the prototype object contained in the StandardSetController class.

The objective of this example

Here we will use a list controller, which will track two sets of records:

  • A primary list that will have all the records selected by the filter
  • A secondary list containing those records the user selected.

The secondary list is created on a standard listview page where a particular user can checkboxes to select the records. The user can then click on a custom list button that navigates to the custom mass update page, which will ideally use the prototype object to apply new field values to the user's selection. The prototype object operates on all the records in the user's selection. To retrieve the prototype object within the custom controller, use the StandardSetController's "getRecord" method.

Implementing this example

We can implement this solution with the steps listed below.

  • Create a Visualforce page called massupdatestages.
  • Provide the following controller
public class selectedSizeWorkaround {
    ApexPages.StandardSetController setCon;
    public selectedSizeWorkaround(ApexPages.StandardSetController controller) {
        setCon = controller;
    }

    public integer getMySelectedSize() {
        return setCon.getSelected().size();
    }

    public integer getMyRecordsSize() {
        return setCon.getRecords().size();
    }
}
  • In the next step, code the markup as shown below.
<apex:page
    standardController="Opportunity"
    recordSetVar="opportunities"
    extensions="selectedSizeWorkaround"
    showHeader="false"
    id="muopp">

    <apex:form id="muform">
        <apex:pageMessage
            summary="Selected Collection Size: {!mySelectedSize}"
            severity="info"
            id="mupms"/>

        <apex:pageMessage
            summary="Record Set Size: {!myRecordsSize}"
            severity="info"
            id="mupmr"/>

        <apex:pageBlock title="Opportunity Mass-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!opportunity.stagename}" id="stagename">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>

        <apex:pageBlock title="Selected Opportunities" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                <apex:column value="{!opp.name}" id="oppname"/>
                <apex:column value="{!opp.stagename}" id="oppstage"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • In the next step, go to object management settings for opportunities,
  • Go to Buttons -> Links -> Actions.
  • Click on New Button or Link.
  • Set the Button Label to "Mass Update Stages", and set the Name to "MassUpdateStages".
  • Now set the Display Type to List Button (ensure that Display Checkboxes (for Multi-Record Selection) is checked).
  • Set the Behavior to Display in the existing window with the sidebar
  • Set the Content Source to Visualforce Page.
  • Click the name of the page you have created just now so that it can be associated with this button.
  • Click on Save.
  • Now go back to the object management settings for opportunities, then go to Search Layouts. Then click on Edit (It will be located next to Opportunities List View).
  • Under Custom Buttons, move the Mass Update Stages button to the Selected Buttons list.
  • Click on Save.
  • Now, click on the Opportunities tab.
  • Select or create a filter that will show existing opportunities that could be changed.
  • We will see checkboxes next to each of the results.
  • Click any number of checkboxes and click the Mass Update Stages button to change the selected stages to any value you wish.
  • Click on Save.

We can update any number of fields using the functionality shown in this demonstration.