1

My VF Page :

<apex:page controller="MyFirstCustomExpenseController">
   <apex:form >
    <apex:pagemessages />
   <apex:outputpanel id="op"> //Line 1
     <apex:pageblock title="sdfsdfsdf" id="pgb"> //Line 2
      <apex:pageblocksection columns="2">
       <apex:outputfield value="{!Expense.Id}" />
       <apex:inputField value="{!Expense.Expense_Date__c}"/>
       <apex:inputField value="{!Expense.Amount__c}"/>
      <apex:inputField value="{!Expense.Type__c}"/>
     <apex:inputField value="{!Expense.Sub_Type__c}"/>
       <apex:inputfield value="{!Expense.Comments__c}" id="fkc"/>
    </apex:pageblocksection>
  <apex:pageblockButtons >
   <apex:commandButton action="{!save}" value="Update" id="stb" rerender="op"/> //Line 3
   <apex:commandbutton action="{!test_pass}" value="Test Pass" />
  </apex:pageblockButtons>
   </apex:pageblock>
</apex:outputpanel>
</apex:form>
</apex:page>

Controller class

  public class MyFirstCustomExpenseController {

        public PageReference test_pass()  {
           ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Record Id - '+exp.Id));   
            return null;
        }


    private final Expense__c exp;

    public MyFirstCustomExpenseController() {
      exp = [SELECT ID,Name,Amount__c,Type__c,Sub_Type__c,Expense_Date__c,Comments__c FROM Expense__c WHERE ID = :ApexPages.CurrentPage().GetParameters().Get('id')];
    }

    public Expense__c getExpense() {
        return exp;
    }

    public PageReference save() {

        if(exp.Comments__c != ''){
          ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info,'Comments should be null'));}else{
           update exp;
       }
      return null;
    }
}

Objective :

In the VF page when an user tries to update an expense with non-null value in Comments an error message should be thrown and the outputpanel containing the data elements should be redrawn.

As soon as I put this rerender attribute in command button

<apex:commandButton action="{!save}" value="Update" id="stb" rerender="op"/> //Line 3

nothing happens :(

Neither the error message is shown to the user nor the output panel is getting refreshed.

Can someone tell me what I am missing ?

1
  • If you put the rerender attribute on the button surely it will rerender the command button and not the output panel. You need to create in your controller a method that will react to the user clicking the button and then rerender the output panel in the code. Commented Dec 27, 2014 at 14:06

2 Answers 2

2

Couple of things I observed and may help to resolve the bug

1)No setter defined for your Expense variable.This may give you false expression that reRender is not happening

public void setExpense(Expense__c exp) {
    this.exp= exp;
}

2)the pagemessage tag needs to inside outputPanel to reflect error message when Rerender happens

  <apex:outputpanel id="op">
      <apex:pagemessages /><!--use pagemessage inside outputPanel thats reRendered-->
0

You will have to add another outputpanel with tags to show updated values from database. will only show values typed by you.

<apex:page controller="ExpenseCustomController">
  <apex:form>
    <apex:outputPanel id="Panel1">
      Expense Date: <apex:inputField value="{!expense.expense_date__c}"/>
      Expense Amount: <apex:inputField value="{!expense.expense_amount__c}"/>
    </apex:outputPanel>
    <apex:commandButton value="Update" action="{!save}" rerender="Panel2"/>
    <br/>
    <apex:outputPanel id="Panel2">
      <apex:outputField value="{!expense.expense_date__c}"/>
      <br/>
      <apex:outputField value="{!expense.expense_amount__c}"/>
    </apex:outputPanel>
  </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .