[SalesForce] Clear inputs after save

i want to clear the input field values after clicking on save button in custom visualforce page.

Visualforce

<apex:page controller="Pagination_min"  showHeader="false" sidebar="false"  extensions="AccountController">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection columns="1"  title="Accounts"> 
            <apex:pageBlockTable value="{!accounts}" var="a">
                <apex:column >
                   <apex:outputLink title="" value="/{!a.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;

                </apex:column>
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.BillingCity}"/>
                <apex:column value="{!a.BillingState}"/>
                <apex:column value="{!a.BillingCountry}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
            <apex:commandButton value="|<" action="{!setcon.first}" disabled="{!!setcon.hasPrevious}" title="First page"/>
            <apex:commandButton value="<" action="{!setcon.previous}" disabled="{!!setcon.hasPrevious}" title="Previous page"/>
            <apex:commandButton value=">" action="{!setcon.next}" disabled="{!!setcon.hasNext}" title="Next page"/>
            <apex:commandButton value=">|" action="{!setcon.last}" disabled="{!!setcon.hasNext}" title="Last page"/>

             <apex:outputText >{!(setCon.pageNumber * size)+1-size}   -    {!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton value="Refresh" action="{!refresh}" title="Refresh Page"/>
            </apex:panelGrid>
        </apex:pageBlockSection>
     </apex:pageBlock>
                      <apex:pageBlock title="My Content" mode="edit">
                          <apex:pageBlockButtons >
                               <apex:commandButton action="{!save}" value="Save" reRender="pb"/>
                          </apex:pageBlockButtons>
                               <apex:pageBlockSection title="My Content Section" columns="2">
                                    <apex:inputField value="{!account.name}" required="false"/>
                                    <apex:inputField value="{!account.type}"/>
                                    <apex:inputField value="{!account.Billingcity}"/>
                                    <apex:inputField value="{!account.billingstate}"/>
                                    <apex:inputField value="{!account.billingcountry}"/>
                               </apex:pageBlockSection>
                      </apex:pageBlock>
  </apex:form>               
</apex:page>

Apex

public class Pagination_min {

    Public Integer noofRecords {get; set;}
    public integer size {get; set;}

    public Apexpages.standardsetController setcon{
        get{
            if(setCon == null){
                size = 10;
                String queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setcon = new apexpages.standardsetController(Database.getquerylocator(queryString));
                setcon.setpagesize(size);
                noofRecords = setcon.getResultsize();
            }
            return setcon;
        }
         set;
    }
    Public list<Account> getAccounts(){
        list<Account> acclist = new list<Account>();
         for(Account ac : (list<Account>)setcon.getrecords()){
             acclist.add(ac);
         }
        return accList;


    }
   Public PageReference Refresh(){

        setcon=null;
        getAccounts();
        setcon.setpageNumber(1);

        return null;
    }
}
public with sharing class AccountController {

    public Account account{get; set;}
    public AccountController(Pagination_min controller) {
      account = new Account();
    }

    public void save(){
        upsert account;
    }
}

Best Answer

The account value is still available in the view state. The following should help you.

public PageReference save(){
    try {
        upsert account;
        account = null;
        return null;
    } catch (NullPointerException npe) {
         System.debug('The following exception has occurred: ' + e.getMessage());
         return null;
}

Hope this helps.

Related Topic