+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

VisualForce page wont save with Controller extension

Hi all,
I have created a visualforce page that allows multiple rows of the same fieldto be added on the page.

However when i try to save the record, the page refreshes but does not save. I am not sure if I have to create a custom save method or weather I can use the one provided with the standard controller.

My controller is:
public class IncidentLogPopup
{ 
    public List<Incident_Log__c> memberList {get;set;}
    public List<Incident_Log__c> memberAddList {get;set;}
    public String memberName {get;set;}  
    public IncidentLogPopup(ApexPages.StandardController controller) {
        String sql = 'SELECT Date_Time__c, Incident__c, Reported_to__c, Reported_By__c FROM Incident_Log__c';
        memberList = Database.Query(sql);
        memberAddList = new List<Incident_Log__c>();
        memberAddList.add(new Incident_Log__c());
    }

  
    public void AddRow()
    {
        memberAddList.add(new Incident_Log__c());
    } 
    
    
}

VF Page:
<apex:page sidebar="false" StandardController="Incident_Log__c" extensions="IncidentLogPopup">
<apex:form >
    <apex:pageBlock id="membAdd" >  
    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Save" Action="{!save}" />
                <apex:commandButton value="Cancel" action="{!cancel}"/>
                </apex:pageBlockButtons>                
        <apex:pageblockSection >
            <apex:pageBlockTable value="{!memberAddList}" var="memb">
                <apex:column headerValue="Date/Time">
                    <apex:inputField value="{!memb.Date_Time__c}"/>
                </apex:column>
                <apex:column headerValue="Incident">
                    <apex:inputField value="{!memb.Incident__c}"/>
                </apex:column>
                <apex:column headerValue="Reported To">
                    <apex:inputField value="{!memb.Reported_To__c}"/>
                </apex:column>
                <apex:column headerValue="Reported By">
                    <apex:inputField value="{!memb.Reported_By__c}"/>
                </apex:column>
            </apex:pageBlockTable> 
            <br/><apex:commandLink value="Add Row" action="{!addRow}" />        
        </apex:pageblockSection>        
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
           
            </apex:pageblockSectionItem>        
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Thanks
Best Answer chosen by Andrew Hoban 6
Virendra ChouhanVirendra Chouhan
Hi
James is 100% correct.

Use the easier approach override the save method in extension.
Copy and paste this method in your Extension
i.e.
Public Pagereference Save(){
        
        insert memberAddlist;
        return null;
    }

 

All Answers

James LoghryJames Loghry
Your page as is will only support 1 record with the save call, even though you've added an extension, etc. Without specifying the "recordSetVar" attribute, the standard controller / save method will only try to save a single record (either updating a record if the id parameter was set or creating a new record if the id parameter was not set).  

You can try playing around with the recordSetVar attribute to see if that will allow you to insert your multiple records.  I think the easier approach though is to override the save method in your extension, and then insert (or upsert?) the list of records.
Andrew Hoban 6Andrew Hoban 6
Thanks for your quick reply. In terms of overriding the save method in my extension, how is the best way to do this? I have never done this before. 

Thanks
Virendra ChouhanVirendra Chouhan
Hi
James is 100% correct.

Use the easier approach override the save method in extension.
Copy and paste this method in your Extension
i.e.
Public Pagereference Save(){
        
        insert memberAddlist;
        return null;
    }

 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thanks to both this has been resolved.
Virendra ChouhanVirendra Chouhan
anytime !!!
And please mark them as solution so if anyone else face the same problem so they got the solution easyly.