0

I need to make VF page for inserting new Contacts that shows pageBlockTable of all existing Contacts below. After i add recordSetVar="contacts" save button stops working.

<apex:page standardController="Contact" recordSetVar="contacts">
    <apex:form >
       <apex:pageBlock title="New Contact">
            <apex:pageblockSection columns="1">
            <apex:inputField value="{! Contact.FirstName }"/>
            <apex:inputField value="{! Contact.LastName }"/>        
            <apex:inputField value="{! Contact.Email }"/> 
       </apex:pageblockSection>
              <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
             </apex:pageBlockButtons>
        </apex:pageBlock>
         <apex:pageBlock title="Contact list">
        <apex:pageBlockTable value="{! contacts }" var="ct">
            <apex:column value="{! ct.FirstName }"/>
            <apex:column value="{! ct.LastName }"/>
            <apex:column value="{! ct.Email }"/>
            <apex:column value="{! ct.Account.Name }"/>
        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form >
</apex:page>
0

You cannot make use of Standard Save functionality to create a new record when you are working with StandardListController i.e recordSetVar. Since the page contains all the records returned from recordSetVar.

To achieve your requirement , you need to create a custom controller which returns StandardSetController containing list of records and overriding the Save function as below.

VisualForcePage :

<apex:page Controller="CustomAccountController" >
    <apex:form >
        <apex:pageBlock >
            <apex:inputtext value="{!AccountName}"/>
            <apex:commandButton value="Save" action="{!SaveRecord}"/>
        </apex:pageBlock>

        <apex:pageBlock title="Accounts">
            <apex:pageBlockTable value="{!Accounts}" var="acc">
                <apex:column value="{!acc.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public with sharing class CustomAccountController 
{

    public string AccountName {get;set;}

    public ApexPages.StandardSetController setCon 
    {
        get {
            if(setCon == null) 
            {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM Account Order By Name asc]));
            }
            return setCon;
        }
        set;
    }

    public List<Account> getAccounts() 
    {
        return (List<Account>) setCon.getRecords();
    }

    public PageReference SaveRecord() 
    {
        Account newAccount = new Account();
        newAccount.Name = AccountName;
        insert newAccount;

        PageReference pageRefer = new PageReference('/apex/CustomAccountsPage');
        pageRefer.setRedirect(true);
        return pageRefer;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.