Salesforce gives you some native bulk edit capabilities right out of the box. Excluding imports and Data Loader options, it’s generally something like selecting a checkbox next to records in a list view and performing a mass delete or the like. But let’s say you have a requirement to give users the ability to enter in some data for records as they might do in a typical spreadsheet environment. You can use Visualforce to construct a fairly simple interface for this.
In this example we have two custom objects with a Master-Detail relationship: the parent, Channel_Partner_QTR_Finance_c, and the child, Channel_Partner_Week_Sellthru_c. Here’s our custom controller for the page.
public class BulkWeeklyEdit {
List<Channel_Partner_Week_Sellthru__c> weeklies;
Id theCPQF;
// class constructor
public SMART_BulkWeeklyEdit() {
theCPQF = ApexPages.currentPage().getParameters().get('id');
weeklies = getWeeklies();
}
// get all the children for this Channel_Partner_QTR_Finance_c record
public List<Channel_Partner_Week_Sellthru__c> getWeeklies() {
if( weeklies == null ) {
List<Channel_Partner_Week_Sellthru__c> cpweeks =
new List<Channel_Partner_Week_Sellthru__c>();
// a cool short cut where you can actually combine the iteration with the query
for ( Channel_Partner_Week_Sellthru__c week : [
SELECT Name, Weighting_Factor__c, Gross_Sellthru_FCST__c,
Contra_FCST__c, Net_Sellthru_FCST__c, Week_Start_Date__c, Week_End_Date__c
FROM Channel_Partner_Week_Sellthru__c
WHERE CPQF_ID__r.Id = :theCPQF ] ) {
cpweeks.add( week );
}
return cpweeks;
} else {
return weeklies;
}
}
// the Save button's method. This will update both the parent and children records.
public PageReference saveWeeklies() {
update weeklies;
Channel_Partner_QTR_Finance__c cpqfToUpdate =
new Channel_Partner_QTR_Finance__c( Id = theCPQF );
update cpqfToUpdate;
return new PageReference('/' + theCPQF);
}
}
Ok, so now let’s build our bulk edit user interface:
<!-- Tie to our custom controller and use our custom parent's object style -->
<apex:page controller="BulkWeeklyEdit" tabstyle="Channel_Partner_QTR_Finance__c">
<apex:form id="theForm">
<apex:pageBlock title="Weekly Forecasting and Performance Data" mode="edit">
<!-- our Save button tied to our Save method -->
<apex:pageBlockButtons >
<apex:commandButton action="{!saveWeeklies}" value="Save"/>
</apex:pageBlockButtons>
<!--
Here we create a table using the Visualforce tag <apex:pageBlockTable>
{!weeklies} calls "getWeeklies" method in our controller.
Note: the word "get" in accessor methods is implicit in Visualforce.
We're also assigning a name variable of "currentWeekly" for each accessed record.
The IDs need to be present to perform the updates but we can keep them
invisible to our user through rendered="false"
-->
<apex:pageBlockTable value="{!weeklies}" var="currentWeekly" id="theRepeat">
<apex:outputText rendered="false">{!currentWeekly.CPQF_ID__r.Id}</apex:outputText>
<apex:outputText rendered="false">{!currentWeekly.CPQF_ID__c}</apex:outputText>
<!--
We create our columns, along with headers, for our user worksheet
Depending on whether or not we want to allow the user to edit a field, we use
the inputField or outputField tags.
-->
<apex:column ><apex:facet name="header">Name</apex:facet>
<apex:outputField value="{!currentWeekly.name}"/>
</apex:column>
<apex:column width="80px"><apex:facet name="header">Weight</apex:facet>
<apex:inputField value="{!currentWeekly.Weighting_Factor__c}" style="width:80px"/>
</apex:column>
<apex:column ><apex:facet name="header">Gross Sellthru FCST</apex:facet>
<apex:outputField value="{!currentWeekly.Gross_Sellthru_FCST__c}"/>
</apex:column>
<apex:column ><apex:facet name="header">Contra FCST</apex:facet>
<apex:outputField value="{!currentWeekly.Contra_FCST__c}"/>
</apex:column>
<apex:column ><apex:facet name="header">Net Sellthru FCST</apex:facet>
<apex:outputField value="{!currentWeekly.Net_Sellthru_FCST__c}"/>
</apex:column>
<apex:column ><apex:facet name="header">Start Date</apex:facet>
<apex:outputField value="{!currentWeekly.Week_Start_Date__c}"/>
</apex:column>
<apex:column ><apex:facet name="header">End Date</apex:facet>
<apex:outputField value="{!currentWeekly.Week_End_Date__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Notice, we made one field, Weighting_Factor__c, available for user entry.
So here’s what the final product looks like:
Happy cloud computing ))))))
Related posts:
- Visualforce - Salesforce.com’s markup language Visualforce is SFDC’s custom mark up language and represents the...
- Adding a custom wizard feature in Salesforce Wizards generally consist of a user interface with step-by-step instructions....
- The User, Visualforce and Apex — about a drop down menu Let’s say you coded a custom view or report using...
Related posts brought to you by Yet Another Related Posts Plugin.











|
Posted by tburre on April 6, 2009 at 12:48 pm
