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:
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.
We can implement this solution with the steps listed below.
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();
}
}
<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>
We can update any number of fields using the functionality shown in this demonstration.