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
thinhtvuthinhtvu 

Saving with PageBlockTable and controller extensions

I have a VisualForce page with a pageBlockTable element that lists a set of contacts that relate to an Opportunity, however, when I hit save on the page, none of my fields are actually saved within the object. Could someone help me?

 

Here's the VF page:

 

<apex:page extensions="drfExtension" standardController="Opportunity" showheader="false">
    <apex:composition template="patriotHeader">
    <apex:define name="header">
        
        <div class="page-title">
            Document Request Form
        </div>      
               
    <apex:form id="drfStep2">
        <apex:pageBlock title="Step 2">
            <apex:pageBlockSection title="CUSTOMER INFORMATION: " collapsible="false">     
                <apex:pageBlockTable value="{!account.Contacts}" var="c">
                    <apex:column value="{!c.FirstName}"/>
                    <apex:column value="{!c.LastName}"/> 
                    <apex:column value="{!c.MailingStreet}"/>
                    <apex:column value="{!c.MailingCity}"/>
                    <apex:column value="{!c.MailingState}"/>
                    <apex:column value="{!c.MailingPostalCode}"/>
                    <apex:column value="{!c.Phone}"/> 
                    <apex:column value="{!c.MobilePhone}"/> 
                    <apex:column value="{!c.Title}"/>  
                </apex:pageBlockTable>
            <apex:inlineEditSupport event="ondblClick"  showOnEdit="saveButton,cancelButton"/> 
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="top" style="text-align:right"> 
                <apex:commandButton value="Save" action="{!save}" id="saveButton" style=""/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton" />
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="bottom" style="text-align:right">
                <apex:CommandButton value="Previous" action="{!goToStep1}"/>
                <apex:commandButton value="Next" action="{!goToStep3}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    </apex:define>
    </apex:composition>
</apex:page>

 And here's the Apex code:

public class drfExtension {

    // Declare the class variables maintaining the state of the wizard.
    // When users enter data into wizard, input is stored in variables.
    
    public Opportunity opp {get; set;}

    public ApexPages.StandardController accountController {get; set;}
    public Account account {get; set;}
    
    public ApexPages.StandardSetController contactController {
        get {
            if(contactController == null) {
                contactController = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Id, FirstName, LastName, MailingStreet, MailingCity, MailingState,
                    MailingPostalCode, Phone, MobilePhone, Title from Contact 
                    WHERE AccountId = :opp.AccountId]));
            }
            return contactController;
        }
        set;
    }
    public List<Contact> contact {get; set;}

    // Creates the controller for the extension.
        
    public ApexPages.StandardController opportunityController;
    public drfExtension(ApexPages.StandardController std)
    {
        opportunityController = std;       
        
        opp = [SELECT AccountId from Opportunity WHERE Id = :std.getId()];
        account = [SELECT Id, Name, Phone, Fax, BillingStreet, BillingCity, BillingState,
            BillingPostalCode FROM Account Where Id = :opp.AccountId];
        contact = [SELECT Id, FirstName, LastName, MailingStreet, MailingCity, MailingState,
            MailingPostalCode, Phone, MobilePhone, Title from Contact where AccountId = :opp.AccountId];
            
        accountController = new ApexPages.StandardController(account);
        contactController = new ApexPages.StandardSetController(contact);
    }
    
    // Methods to return one of the three class variables.
    
    public Account getAccount(){
        return account;
    }
    
    public List<Contact> getContact(){
        return (List<Contact>) contactController.getRecords();
    }

    // Methods that manage the navigation of the wizard.

    public PageReference goToStep1() {
        Pagereference pg1=new pagereference('/apex/drfStep1?oppid='+opp.id);
        return pg1;
    }


    public PageReference goToStep2() {
        Pagereference pg2=new pagereference('/apex/drfStep2?oppid='+opp.id);
        return pg2;
    }
    
    public PageReference goToStep3() {
        Pagereference pg3=new pagereference('/apex/drfStep3?oppid='+opp.id);
        return pg3;
    }
    
    public PageReference goToStep4() {
        
        Pagereference pg4=new pagereference('/apex/drfStep4?oppid='+opp.id);
        return pg4;
    }
 
    // Methods to alter the state of each object's data.
    
    public PageReference save()
    {
        opportunityController.save();
        accountController.save();
        contactController.save();
        return null;
    }
    
        public PageReference cancel()
    {
        opportunityController.cancel();
        accountController.cancel();
        contactController.cancel();
        return null;
    }
    
}

 

Anup JadhavAnup Jadhav

hey there,

 

I'd create a new public instance variable called "contacts" which is a List<Contact>.

 

Then in the constructor i'd retrieve all of the contacts for the corresponding account

 

contacts = [Select Id, <other fields> from Contact where Id = :account.Id];

 

Use this contacts variable in the pageblock table, and you should be okay.

 

Thanks,

Anup