0
public class contact_fetch_List_method_controller{
public contact consave{get;set;}

public contact_fetch_List_method_controller(){
    consave = new contact();
}

public pageReference Save(){
    if(consave.lastname == null){
        ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'Last name required'));
        return null;
    }
    else{
        insert consave;
        consave = new contact();
    }
    return null;
}
public list<Contact> getCont(){
    List<Contact> conList = new List<Contact>();
    List<Contact> con = [SELECT Id,firstname, lastname, email ,phone FROM Contact Order by LastModifiedDate DESC];

    for(Contact conRecord : con){
         conList.add(conRecord);
     }
    return conList;
}
}

page code

<apex:page controller="contact_fetch_List_method_controller">
  <apex:form>
      <apex:pageBlock id="bgBlock">
          <apex:pageMessages/>
          <apex:pageBlockSection collapsible="true" columns="2" id="pgSection">
              <apex:inputField value="{!consave.firstname}"/>
              <apex:inputField value="{!consave.lastname}"/>
              <apex:inputField value="{!consave.email}"/>
              <apex:inputField value="{!consave.phone}"/>
              <apex:commandButton value="Save" action="{!save}" reRender="pbTable,pgSection"/>          
          </apex:pageBlockSection>

          <apex:pageBlockTable id="pbTable" value="{!Cont}" var="condata">
              <apex:column headerValue="FirstName" value="{!condata.firstname}"/>
              <apex:column headerValue="LastName" value="{!condata.lastname}"/>
              <apex:column headerValue="email" value="{!condata.email}"/>
              <apex:column headerValue="Phone" value="{!condata.phone}"/>          
          </apex:pageBlockTable>
      </apex:pageBlock>
    </apex:form>
</apex:page>
1
  • 1
    You have a VF table of Contacts bound to a list: {!Cont} but your save method references conSave -- we're missing something here to help
    – cropredy
    Jul 30, 2016 at 19:33

1 Answer 1

1

You need to rerender the pagemessage from your save button click event

  1. Define id for pageMessage. i.e. pgMsg
  2. add pgMsg to Save rendered attribute.

    <apex:page controller="contact_fetch_List_method_controller">
     <apex:form >
      <apex:pageBlock id="bgBlock">
          <apex:pageMessages id="pgMsg" />
          <apex:pageBlockSection collapsible="true" columns="2" id="pgSection">
              <apex:inputField value="{!consave.firstname}"/>
              <apex:inputField value="{!consave.lastname}"/>
              <apex:inputField value="{!consave.email}"/>
              <apex:inputField value="{!consave.phone}"/>
              <apex:commandButton value="Save" action="{!save}" reRender="pbTable,pgSection,pgMsg"/>          
          </apex:pageBlockSection>
    
          <apex:pageBlockTable id="pbTable" value="{!Cont}" var="condata">
              <apex:column headerValue="FirstName" value="{!condata.firstname}"/>
              <apex:column headerValue="LastName" value="{!condata.lastname}"/>
              <apex:column headerValue="email" value="{!condata.email}"/>
              <apex:column headerValue="Phone" value="{!condata.phone}"/>          
          </apex:pageBlockTable>
      </apex:pageBlock>
    </apex:form>
    

You must log in to answer this question.

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