+ Start a Discussion
deepakpdeepakp 

saving only changed records for mass update VF page

I have written VF page which allows user to edit multiple records.
 
In save method in my controller, I am calling update oppportunityList.
 
This saves all records.
I want to save only records that have been changed. Is there any standard functionality in visualforce to detect only changed records.
mtbclimbermtbclimber
You can maintain the state of the values you originally sent down and compare the values sent back OR try the standard set controller which does this for you automatically.
deepakpdeepakp
I am using custom controller and displaying records as below.
 
 
<apex:pageBlockTable value="{!oppList}" var="opp" rendered="{!NOT(ISNULL(oppList))}">
<apex:column headerValue="Manager Commit">
<apex:inputField value="{!opp.Manager_Commit__c}"/>
</apex:column>
<apex:column headerValue="Manager Commit Comments">
<apex:inputField value="{!opp.Manager_Commit_Comments__c}"/>
</apex:column>
<apex:column value="{!opp.Probability}"/>
<apex:column value="{!opp.StageName}"/>
<apex:column value="{!opp.CloseDate}"/>
<apex:column value="{!opp.Owner.Name}"/>
<apex:column value="{!opp.Account.Name}"/>
<apex:column value="{!opp.Name}"/>
<apex:column value="{!opp.Fulfillment_Partner__c}"/>
<apex:column value="{!opp.Amount}"/>
</apex:pageBlockTable>
I need to store origional values in the list of hidden variables and compare it against changed list.
<apex:inputHidden value="{!inputValue}" id="theHiddenInput"/> only store single value in inputValue member variable in controller.
 
How to record # and store it in hidden variable for  <Apex:pageBlockTable> loop?
deepakpdeepakp
I did this processing on server side in controller class
 
 public PageReference save() {
        try {
            changedOppList=new List<Opportunity>();
            for ( Opportunity o: oppList) {
                Opportunity o2=origOppMap.get(o.Id);
                if ( o2 != null &&
                    (o2.Manager_Commit__c != o.Manager_Commit__c  ||
                     o2.Manager_Commit_Comments__c != o.Manager_Commit_Comments__c) ) {
                     changedOppList.add(o);
                }
            }
            update changedOppList;
        } catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        return null;
    }

mtbclimbermtbclimber
Doesn't look like you are doing anything significant in your save routine. As I mentioned you might want to check out the standard set controller. Try this page:

Code:
<apex:page standardController="Opportunity" recordSetVar="opportunities">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
            <apex:pageMessages/>
            <apex:pageBlockTable value="{!selected}" var="opp">
                <apex:column headerValue="Manager Commit">
                    <apex:inputField value="{!opp.Manager_Commit__c}"/>
                </apex:column>
                <apex:column headerValue="Manager Commit Comments">
                    <apex:inputField value="{!opp.Manager_Commit_Comments__c}"/>
                </apex:column>
                <apex:column value="{!opp.Probability}"/>
                <apex:column value="{!opp.StageName}"/>
                <apex:column value="{!opp.CloseDate}"/>
                <apex:column value="{!opp.Owner.Name}"/>
                <apex:column value="{!opp.Account.Name}"/>
                <apex:column value="{!opp.Name}"/>
                <apex:column value="{!opp.Fulfillment_Partner__c}"/>
                <apex:column value="{!opp.Amount}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
It's set up to work on the "selected" records which means it's designed to be tied to a list button invoked from a standard list or search results page where the user selects one or more values and the clicks the button.

If you change the binding to "{!opportunities}" instead of "{!selected}" and call the page directly it will operate on all the results of the last selected view/filter.