2

I have an input form with some fields that enable entering data about students. I also have a save button and another form on the same page which is supposed to show a table where all the saved records are being shown. I need help with showing of each saved record in the table below. What it does now is it shows only the current record saved. When I enter a new record, it saves in the database and it shows in the table but all the previous saved records do not show.

I am a very beginner with SalesForce and I really don't know how to proceed further. Any help would be greatly appreciated.

My Apex code is this:

public with sharing class CustomStudentController 
 {


 public List<Student__c> students {get;set;}

   public Student__c student{ 
     get {
        if (student == null)
            student = new Student__c();
      return student;
      }
     set;
   }


public ApexPages.StandardSetController setCon 
{
    get {
        if(setCon == null) 
        {
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name, Priimek__c, Letnik__c, Naslov__c, Datum_rojstva__c, Naziv_fakultete__c, Studijski_program__c, Tip_studija__c FROM Student__c Order By Name asc]));
        }
        return setCon;
    }
    set;
}


public List<Student__c> getStudents() 
{
    return (List<Student__c>) setCon.getRecords();
}

public CustomStudentController(){
    students = new List<Student__c>();
    student = new Student__c();
}

public PageReference add(){
    students.add(student);
    student = new Student__c();
    return null;
}

public PageReference SaveRecord() 
{

    insert students;

   /* PageReference pageRefer = new PageReference('/apex/StOver');
    pageRefer.setRedirect(true); */
    return null; 
    } 
}

My VisualForce page is this:

<apex:page controller="CustomStudentController">
<apex:form id="form1">
  <apex:pageBlock title="Nov Student">
     <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Student.Name}"/>
        <apex:inputField value="{!Student.Priimek__c}"/>
        <apex:inputField value="{!Student.Letnik__c}"/>
        <apex:inputField value="{!Student.Naslov__c}"/>
        <apex:inputField value="{!Student.Datum_rojstva__c}"/>
        <apex:inputField value="{!Student.Naziv_fakultete__c}"/>
        <apex:inputField value="{!Student.Studijski_program__c}"/>
        <apex:inputField value="{!Student.Tip_studija__c}"/>
        <apex:selectcheckboxes layout="pagedirection">
           <apex:selectOption id="Placnik" itemValue="Samoplacnik" itemLabel="Samoplacnik" />
        </apex:selectcheckboxes>
        <apex:pageBlock title="Vsi studenti">
           <apex:pageBlockTable value="{!Student}" var="wrapper" rows="5">
              <apex:column value="{!wrapper.Name}"/>
              <apex:column value="{!wrapper.Priimek__c}"/>
              <apex:column value="{!wrapper.Naslov__c}"/>
              <apex:column value="{!wrapper.Datum_rojstva__c}"/>
              <apex:column value="{!wrapper.Naziv_fakultete__c}"/>
              <apex:column value="{!wrapper.Studijski_program__c}"/>
              <apex:column value="{!wrapper.Letnik__c}"/>
              <apex:column value="{!wrapper.Tip_studija__c}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!SaveRecord}" value="Save" id="theButton"/>
     </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
 </apex:page>

1 Answer 1

1

Your just calling Single student in pageBlockTable , Call students inside of student to show all students. Like below code

<apex:page controller="CustomStudentController">
<apex:form id="form1">
  <apex:pageBlock title="Nov Student" id="mainSection">
     <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Student.Name}"/>
        <apex:inputField value="{!Student.Priimek__c}"/>
        <apex:inputField value="{!Student.Letnik__c}"/>
        <apex:inputField value="{!Student.Naslov__c}"/>
        <apex:inputField value="{!Student.Datum_rojstva__c}"/>
        <apex:inputField value="{!Student.Naziv_fakultete__c}"/>
        <apex:inputField value="{!Student.Studijski_program__c}"/>
        <apex:inputField value="{!Student.Tip_studija__c}"/>

        <apex:selectcheckboxes layout="pagedirection">
           <apex:selectOption id="Placnik" itemValue="Samoplacnik" itemLabel="Samoplacnik" />
        </apex:selectcheckboxes>
      </apex:pageBlockSection>
      <apex:pageBlockSection columns="1">
        <apex:pageBlock title="Vsi studenti"  >
           <apex:pageBlockTable value="{!Students}" var="wrapper" >
              <apex:column value="{!wrapper.Name}"/>
              <apex:column value="{!wrapper.Priimek__c}"/>
              <apex:column value="{!wrapper.Naslov__c}"/>
              <apex:column value="{!wrapper.Datum_rojstva__c}"/>
              <apex:column value="{!wrapper.Naziv_fakultete__c}"/>
              <apex:column value="{!wrapper.Studijski_program__c}"/>
              <apex:column value="{!wrapper.Letnik__c}"/>
              <apex:column value="{!wrapper.Tip_studija__c}"/>
              <apex:commandLink action="{! deleteStudentRecordById}"  value="del" reRender="mainSection">
             <apex:param name="studentId" value="{!wrapper.id}"/>
            </apex:commandLink>
           </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton action="{!SaveRecord}" value="Save" id="theButton" reRender="mainSection"/>
     </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
 </apex:page>

Change the controller as follows

public with sharing class CustomStudentController 
{
    public List<Student__c> students {get;set;}
    public Student__c student{get;set;}   

    public ApexPages.StandardSetController setCon {
    get{
     setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name, Priimek__c, Letnik__c, Naslov__c, Datum_rojstva__c, Naziv_fakultete__c, Studijski_program__c, Tip_studija__c FROM Student__c Order By createdDate desc]));
     return setCon;
    }    
    set;} 

    public CustomStudentController(){
        students = (List<Student__c>) setCon.getRecords();
        student = new Student__c();
    }

   public PageReference SaveRecord() 
{        
    insert student;
    student = new Student__c();
    students = (List<Student__c>) setCon.getRecords();
    pagereference pg= Page.vf192786;  //your vf page name   
    pg.setRedirect(true);
    return pg; 
} 
public PageReference deleteStudentRecordById(){
    Id studentId=(id) ApexPages.currentPage().getParameters().get('studentId');
    system.debug('id***********' + studentId);
    database.delete(studentId);      
     pagereference pg= Page.vf192786;     //your vf page name    
    pg.setRedirect(true);
    return pg;
} 

}
10
  • @user911 Controller as updated .
    – NITHESH K
    Sep 20, 2017 at 7:59
  • I am closer to the solution now, I added add in SaveRecord and changed the value to !Students and not !Student as it was. Now it shows the second saved record as well. But when I try to save the third record it gives the following error System.DmlException: Insert failed. First exception on row 0 with id a000Y00000Hk6fFQAR; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error is in expression '{!SaveRecord}' in component <apex:commandButton> in page stover: Class.CustomStudentController.SaveRecord: line 49, column 1. Thanks for the help again.
    – user911
    Sep 20, 2017 at 7:59
  • @user911 Tried with updated solution ?
    – NITHESH K
    Sep 20, 2017 at 8:02
  • Yes, and it already shows the 2nd saved record but for the third it has the id problem (error).
    – user911
    Sep 20, 2017 at 8:02
  • @user911 try now
    – NITHESH K
    Sep 20, 2017 at 8:10

You must log in to answer this question.

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