function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Hoban 14Andrew Hoban 14 

Visualforce Quick Save not working

Hi all,
I have a visualforce page that works great, however i need to implement a quick save button for users to quickly save the fields without having to refresh the page. I have used the standard quick save command button "<apex:commandButton value="Quick Save" action="{!QuickSave}"/>" however when the button is clicked the records are not saved. I have created a custom controller so im not sure if I need to reference the button inside it.

Many thanks

Controller:
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 string searchstring {get;set;} 

    
    public PageReference cancel() {
        return null;
    }


    public IncidentLogPopup() {

    }
    
   
    public IncidentLogPopup(ApexPages.StandardController controller) {
        String sql = 'SELECT Match_Day_Ops__c, Date_Time__c, Date_Closed__c, Type__c, Priority__c, Incident__c, Reported_to__c, Reported_By__c, Opened_Closed_Status__c FROM Incident_Log__c';
        memberList = Database.Query(sql);
        memberAddList = new List<Incident_Log__c>();
        memberAddList.add(new Incident_Log__c(Match_Day_Ops__c = 'a0Gf000000111CV', Date_Time__c = DateTime.Now()));
         
        
    }
    
    public void AddRow()
    {
        memberAddList.add(new Incident_Log__c(Match_Day_Ops__c = 'a0Gf000000111CV', Date_Time__c = DateTime.Now()));
        
    } 
    
    Public Pagereference Save(){
        insert memberAddlist;
        PageReference page = new Pagereference('/a0G/o');
                     page.setRedirect(true);
            return page;
  }
   
    
     

  


        
   }

VF Page:
<apex:page sidebar="false" StandardController="Match_Day_Ops__c"  extensions="IncidentLogPopup">

<style type = "text/css">
        .colHeadr {text-align:center;}     
    </style>

    
 


  <apex:form >
  
  
  

    <apex:pageBlock title="Incident Log {!$ObjectType.Match_Day_Ops__c.Fields.Name.Label}" id="membAdd" >  
    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Save" Action="{!save}" />
                <apex:commandButton value="Quick Save" action="{!QuickSave}"/> 
                <apex:commandButton value="Cancel" action="{!cancel}"/>
                </apex:pageBlockButtons>  
                
                
              
                                  
       <apex:pageblockSection >     
          
   
            <apex:pageBlockTable style="width:100%" value="{!memberAddList}" var="memb">     
                <apex:column headerValue="Match Day Ops" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Match_Day_Ops__c}" style="width:325px" />
                </apex:column>
                <apex:column headerValue="Type" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Type__c}"/>
                </apex:column>
                 <apex:column headerValue="Priority" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Priority__c}"/>
                </apex:column>
                  <apex:column headerValue="Date/Time Opened" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Date_Time__c}"/>
                </apex:column>
                <apex:column headerValue="Incident Open" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Incident__c}" style="width: 200px; height: 80px"/>
                </apex:column>

             <apex:column headerValue="Date/Time Closed" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Date_Closed__c}"/>
                </apex:column> 
                <apex:column headerValue="Incident Closed" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Incident_Closed__c}" style="width: 200px; height: 80px"/>
                </apex:column>
                <apex:column headerValue="Reported To" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Reported_To__c}"/>
                </apex:column>
                <apex:column headerValue="Reported By" headerClass="colHeadr">
                    <apex:inputField value="{!memb.Reported_By__c}"/>
                </apex:column>  
                
                </apex:pageBlockTable> 

        </apex:pageblockSection>    
           
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
<apex:commandLink value="Add Row" action="{!addRow}" reRender="membAdd" />  
           
            </apex:pageblockSectionItem>        
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>

 
Best Answer chosen by Andrew Hoban 14
Shashikant SharmaShashikant Sharma
instead of insert , use upsert
upsert is combination of update and insert, which menas if id exists update if does not then insert.
It will update the record once it has the Id.

All Answers

Shashikant SharmaShashikant Sharma
May be you could try to use normal save but use that in an Action function.
Andrew Hoban 14Andrew Hoban 14
Hi, the normal save does not work. When I save the records once its okay, however a second save will cause an error. I need a way for the records to be saved repeatedly without the page being redirected. Thanks
Andrew Hoban 14Andrew Hoban 14
In my controller I have now created a quicksave method, this works great on the first save, however the second time I have I am getting the error message:

"System.DmlException: Insert failed. First exception on row 0 with id a1Nf0000000lUuoEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!QuickSave}' in component <apex:commandButton> in page incidentlogtest: Class.IncidentLogPopup.quickSave: line 43, column 1
Class.IncidentLogPopup.quickSave: line 43, column 1"




I think this is because the fields are trying to be overwritten and is causing the error. Is there a way around this problem at all?
Shashikant SharmaShashikant Sharma
instead of insert , use upsert
upsert is combination of update and insert, which menas if id exists update if does not then insert.
It will update the record once it has the Id.
This was selected as the best answer