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
Justin PietrasJustin Pietras 

Save button on a VF Page not saving/updating

I created a VF Page using a standard controller and a custom extension.  My purpose for this VF Page is to update fields on a specific record within a custom object, but for some reason it is not updating the record once I enter in the data and click on the save button.  

BELOW IS MY CONTROLLER AND EXTENSION:

public class SupplierInfoExtension{
  public order__c selectedOrder {get;set;}
  public List<Order_Line_Item__c> selectedOrderLines {get;set;}
  
  public SupplierInfoExtension(ApexPages.StandardController controller) {
    selectedOrder = (Order__c) controller.getRecord();
    selectedOrderLines = [SELECT Additional_Information__c, Approx_Lbs_Gal__c, CreatedById, CreatedDate, FOB_DVD_Customer__c, FOB_DVD_Supplier__c, 
                                 Id, Name, Order__c, Product__r.name, Product_Code__c, Customer_Product_Code__c
                            FROM Order_Line_Item__c
                           WHERE Order__c = : selectedOrder.Id];
  }
  public void save()
  {
      update selectedOrder;
  }
}



BELOW IS MY VF PAGE CODE:

<apex:page sidebar="false" StandardController="Order__c" extensions="SupplierInfoExtension">
    <apex:form >
    <apex:pageBlock >
            <apex:pageBlockTable value="{!Order__c}" var="Order">
                <apex:column value="{!Order__c.name}"/>
                <apex:column value="{!Order__c.customer__r.name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="save" action="{!save}"/>
        </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!selectedOrderLines}" var="ol">
                 <apex:column headerValue="Product Code">
                    <apex:inputField value="{!ol.Product_Code__c}"/>
                </apex:column>
            </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Any thoughts on why the record is not updating?  
Best Answer chosen by Justin Pietras
Saurabh BSaurabh B
Hi Justin, here is the example code that I wrote today in my Dev org. This allows us to mass update all Contacts under Account. Just modify this code to your use case and it should work fine. In code below, you should replace "Account" with "order__c" (your parent) and "Contact" with "Order_Line_Item__c" (child) and that should do it .. Let me know if you need any additional help ..

Controller Extension:
public class testextn {

     private ApexPages.StandardController stdController;  

    public Account acct;
    public String currentRecordId {get;set;}
    public List <Contact> Con {get;set;}
    
    public testextn (ApexPages.StandardController stdController) {
       currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        this.acct = (Account)stdController.getRecord();
        Con = [SELECT Firstname,AccountId,LastName FROM Contact WHERE AccountID = 
                    :currentRecordId  ];
        
        
        system.debug(acct); 
    }
    
    public pagereference dosave () {
        update Con;
       
        
        PageReference reRend = new PageReference('/' + currentRecordId );
        reRend.setRedirect(true);
        return reRend;
        
    }
		

}

VF page:
 
<apex:page standardController="Account" extensions="testextn"
         
           tabStyle="Account" sidebar="false">
  <apex:form >
    <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" 
                            action="{!dosave}"/>
      </apex:pageBlockButtons>
      <apex:pageblockSection title="Account Details"> 
      <apex:outputField value="{!Account.Name}"/>
      
      </apex:pageblockSection>
      <apex:pageBlockTable value="{!Con}" 
                           var="c">
        <apex:column headerValue="First name">
          <apex:inputField value="{!c.Firstname}"/>
        </apex:column>
        <apex:column headerValue="Last name">
          <apex:inputField value="{!c.lastname}"/>
        </apex:column>
  <apex:column headerValue="Company name">
          <apex:inputField value="{!c.AccountId}"/>
        </apex:column>
        
      </apex:pageBlockTable>      
    </apex:pageBlock>
  </apex:form>
</apex:page>


 

All Answers

Saurabh BSaurabh B
Hi Justin, intead of using standard save() in your controller, change the name of your method to something else, for example dosave() and use that method in VF page, it should work. At this moment, your code is overriding the standard Salesforce save method which I believe is causing the issue.
Justin PietrasJustin Pietras
Are you saying do the follwoing (see below):

public class SupplierInfoExtension{
  public order__c selectedOrder {get;set;}
  public List<Order_Line_Item__c> selectedOrderLines {get;set;}
  
  public SupplierInfoExtension(ApexPages.StandardController controller) {
    selectedOrder = (Order__c) controller.getRecord();
    selectedOrderLines = [SELECT Additional_Information__c, Approx_Lbs_Gal__c, CreatedById, CreatedDate, FOB_DVD_Customer__c, FOB_DVD_Supplier__c, 
                                 Id, Name, Order__c, Product__r.name, Product_Code__c, Customer_Product_Code__c
                            FROM Order_Line_Item__c
                           WHERE Order__c = : selectedOrder.Id];
  }
  public void dosave()
  {
      update selectedOrder;
  }
}
Saurabh BSaurabh B
Correct. Then use dosave() in your save button like this,

<apex:commandButton value="save" action="{!dosave}"/>
Saurabh BSaurabh B
Also, instead of public void dosave(), use public PageReference dosave()
Justin PietrasJustin Pietras
Still not working.   

Another strange thing is 2 of the fields are required in the VF page, but when I look at them in the custom object they are not required.  That is really strange.  

Any thoughts?
Justin PietrasJustin Pietras
Recieve an error when I try the PageReference dosave()

User-added image
Saurabh BSaurabh B
Hi Justin, try adding 'return null;' after update selectedorder; and see if it works.

Also, could you pls explain what the exact requirements are? Are you trying to update order and order line item at the same time? Just want to understand the use case...
Justin PietrasJustin Pietras
So after adding that, the data stays in the fields of the VF Page form, but does not update the actually database.  

I am trying to update the order line items at the same time on one page, so I dont have to keep clicking from one line item to the next.  
Saurabh BSaurabh B
Hi Justin, here is the example code that I wrote today in my Dev org. This allows us to mass update all Contacts under Account. Just modify this code to your use case and it should work fine. In code below, you should replace "Account" with "order__c" (your parent) and "Contact" with "Order_Line_Item__c" (child) and that should do it .. Let me know if you need any additional help ..

Controller Extension:
public class testextn {

     private ApexPages.StandardController stdController;  

    public Account acct;
    public String currentRecordId {get;set;}
    public List <Contact> Con {get;set;}
    
    public testextn (ApexPages.StandardController stdController) {
       currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        this.acct = (Account)stdController.getRecord();
        Con = [SELECT Firstname,AccountId,LastName FROM Contact WHERE AccountID = 
                    :currentRecordId  ];
        
        
        system.debug(acct); 
    }
    
    public pagereference dosave () {
        update Con;
       
        
        PageReference reRend = new PageReference('/' + currentRecordId );
        reRend.setRedirect(true);
        return reRend;
        
    }
		

}

VF page:
 
<apex:page standardController="Account" extensions="testextn"
         
           tabStyle="Account" sidebar="false">
  <apex:form >
    <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" 
                            action="{!dosave}"/>
      </apex:pageBlockButtons>
      <apex:pageblockSection title="Account Details"> 
      <apex:outputField value="{!Account.Name}"/>
      
      </apex:pageblockSection>
      <apex:pageBlockTable value="{!Con}" 
                           var="c">
        <apex:column headerValue="First name">
          <apex:inputField value="{!c.Firstname}"/>
        </apex:column>
        <apex:column headerValue="Last name">
          <apex:inputField value="{!c.lastname}"/>
        </apex:column>
  <apex:column headerValue="Company name">
          <apex:inputField value="{!c.AccountId}"/>
        </apex:column>
        
      </apex:pageBlockTable>      
    </apex:pageBlock>
  </apex:form>
</apex:page>


 
This was selected as the best answer
Justin PietrasJustin Pietras
Thank you!  I do have a quick question....  If I wanted to switch the parent and child around in this code, would it still do the same thing?  For example, if I was in a line item and i wanted to build a custom page on that object but also wanted to bring in information from the parent account, could this code work?
 
Saurabh BSaurabh B
Yes, you could do that as well however 1 child will have only 1 parent correct? So you will only see two records at the same time. Here is flipped code with StandardController being "Contact"
 
public class testextn {

     private ApexPages.StandardController stdController;  
		 public Contact c;
    public String currentRecordId {get;set;}
    public List <Contact> Con {get;set;}
    
    public testextn (ApexPages.StandardController stdController) {
       currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        this.c = (Contact)stdController.getRecord();
        Con = [SELECT Firstname,AccountId,LastName,Account.Name,Account.CustomerPriority__c FROM Contact WHERE ID = 
                    :currentRecordId  ]; 
        
        
        system.debug(Con); 
    }
    
    public pagereference dosave () {
        update Con;
       
        
        PageReference reRend = new PageReference('/' + currentRecordId );
        reRend.setRedirect(true);
        return reRend;
        
    }
		

}
 
<apex:page standardController="Contact" extensions="testextn"
         
           tabStyle="Account" sidebar="false">
  <apex:form >
    <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" 
                            action="{!dosave}"/>
      </apex:pageBlockButtons>
      <apex:pageblockSection title="Contact Details"> 
          
          <apex:inputField value="{!Contact.Account.Name}"/>
          <apex:inputField value="{!Contact.Account.CustomerPriority__c}"/>

      </apex:pageblockSection>
      <apex:pageBlockTable value="{!Con}" 
                           var="c">
        <apex:column headerValue="First name">
          <apex:inputField value="{!c.Firstname}"/>
        </apex:column>
        <apex:column headerValue="Last name">
          <apex:inputField value="{!c.lastname}"/>
        </apex:column>
  <apex:column headerValue="Company name">
          <apex:inputField value="{!c.AccountId}"/>
        </apex:column>
        
      </apex:pageBlockTable>      
    </apex:pageBlock>
  </apex:form>
</apex:page>

Please mark as Best Answer so that it can help others!