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
Jonathan JacobsJonathan Jacobs 

Customize the save button when editing a table of data

I have a visualforce page I am working on in which I am trying to edit a list of related records in a table format and save all the records with a single save button.  I created a purchase order object and a related purchase order products, which is the individual line item records that will go on the purchase order.  I used the Editing a Table of Data in a Page as my basis for creating the editiable table in visualforce.  The table draws in the related items for the purchase order.  I'm having trouble figuring out how to customize the save button to save the changes in the table.  I believe the save button is using the standard controller and trying to save the purchase order record.  I would like to customize the save button so that it can save the purchase order items in the editable table.  I'm learning how to code and i'm not sure how to set this up.  I'm thinking a controller extension to customize the save button, but I'm not sure how this would be coded.  Any help would be appreciated.  

Here is an example of the page currenlty
Example Purchase Order

And here is my current Visualforce Code
 
<apex:page standardController="Purchase_Order__c" >
    <apex:slds />
    <apex:detail inlineEdit="true"/>
   
    <!--<flow:interview name="AddProduct" />-->
    
    <apex:form> 
	<apex:pageBlock title="Purchase Order Items">
      <apex:pageBlockTable value="{!Purchase_Order__c.Purchase_Order_Products__r}" var="POLI">
      
        <apex:column value="{!POLI.POLI_Part_Number__c}"/>
        <apex:column value="{!POLI.Catalog_Number__c}"/>
        <apex:column value="{!POLI.UOM__c}"/>
        <apex:column value="{!POLI.ordered__c}"/>
        <apex:column headerValue="Backordered">
            <apex:inputField value="{! POLI.Backordered__c }"/>
        </apex:column>
        <apex:column headerValue="Received">
            <apex:inputField value="{! POLI.Received__c }"/>
        </apex:column>
        <apex:column headerValue="RMA">
            <apex:inputField value="{! POLI.RMA__c }"/>
        </apex:column>
      </apex:pageBlockTable>

   <apex:pageMessages />
   <apex:pageBlockButtons>
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlockButtons>

   </apex:pageBlock>
   </apex:form>
</apex:page>

 
Jayant DasJayant Das
The implementation you have does use the standard controller and the standard save operation. If you need to add further customizations to it, consnider using an extension class. Create an extension class with "save" method and then add your customizations. Include the extension in your VF page, you should be good to go.
Jonathan JacobsJonathan Jacobs
Thanks, Jayant.  That's what I was thinking.  I am trying to set up the extension controller, but I don't think I'm doing it correctly.  For starters, I'm getting an unknown cunstructor error  - Unknown constructor 'POLineItemsExtension.POLineItemsExtension(ApexPages.StandardController controller)'. 

Secondly, I don't think I am setting up the save method in the extension controller correctly.  Here is my class code
 
public class POLineItemsExtension {

	public List<POLineItems__c> POLIs{get;set;} 

    public Purchase_Order__c thisPOid {get;set;} 

    public Purchase_Order__c poGetter {get;set;} 

     

    //Constructor 

    public POLineItemsExtension(ApexPages.StandardSetController controller) { 

        poGetter = (Purchase_Order__c)controller.getRecord();      

        thisPOid = [SELECT id FROM Purchase_Order__c WHERE id=: poGetter.id LIMIT 1]; 

        POLIs = [SELECT id,POLI_Part_Number__c,Catalog_Number__c, UOM__c, ordered__c,Backordered__c,Received__c,RMA__c FROM POLineItems__C WHERE PO_Of_Items__c = :poGetter.id];     

} 

    public pageReference lineItemsSave(){  
		update POLIs;
        return pageRef;  

    }    

}

 
Jayant DasJayant Das
You would need to rename "lineItemsSave()" to "save()", so that it is kind of overridden and the container knows which one to call. Let me know if it helps.
Jonathan JacobsJonathan Jacobs
Jayant, 

I have reviewed the link and I'm still having trouble understanding how to set up the extension.  Do you see where my mistake is?  I assume I have to get the ID of the Purchase Order record and then I need to set up a query that returns the line items that are related to the purchase order.  But it seems I'm still missing something since I'm getting the unknown constructor error.  

I updated the button on the visualforce page to "lineItemsSave()" so that it matches the one on the class, but I'm still not getting it to save.  

Thanks for the help! 
Jayant DasJayant Das
Hi Jonathan,

Here's a sample code that lets you write an extension and use that on the VF page. Let me know if this helps.

Notice that I have not made any change on the VF's save method, but added the extension only. And within the extension, I am in this example doing nothing and returning back to the same page where I was without making any actual save on the records.

VF:
<apex:page standardController="Account" extensions="testVFTableExtension" recordSetVar="accounts" 
   tabstyle="account" sidebar="false">
   <apex:form> 
   <apex:pageBlock >
   <apex:pageMessages />
   <apex:pageBlockButtons>
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlockButtons>

   <apex:pageBlockTable value="{!accounts}" var="a">
      <apex:column value="{!a.name}"/>
      
      <apex:column headerValue="Industry">
         <apex:inputField value="{!a.Industry}"/>
      </apex:column>

   </apex:pageBlockTable>
   </apex:pageBlock>
   </apex:form>
</apex:page>

Extension:
public class testVFTableExtension {
    private final Account acct;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public testVFTableExtension(ApexPages.StandardSetController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    
    public PageReference save() {
        // do everything here
        system.debug('Overridden save');
        return null;
    }
}

Thanks,
Jayant