function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sivasankari MuthuSivasankari Muthu 

How to insert multi records into the object

Hi All,
In my scenario,I have two objects Student_Academic and student_score,
Student_Academicobject contains :stuId,Name
User-added image
student_score Contains:stuId,Subject,Marks
User-added image
Page design is
User-added image
After click the save button the relevant marks and subject to that corresponding student store in student_score object

Thanks
VineetKumarVineetKumar
Can you refer your controller here.
You will have to do a bit of manipuations on the record before saving them.
Sivasankari MuthuSivasankari Muthu
Hi VineetKumar,
Thanks for your reply.

vfp is
 
<apex:page showHeader="true" Controller="MultiStudentMarks" id="addScoresPage" sidebar="false" docType="html-5.0">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"/>
      <apex:pageMessages id="PageMsg"/>
       <apex:form id="pageForm">
           <h1>
            <apex:outputText value="Add Scores" style="color:blue;font-size:20px; line-height: 1.5;" /><br/><br/>
            <table style="color:blue; font-size:15px; line-height: 1.0;">
            <tr>
             <td  style="width: 200px; padding-left: 200px; height: 50px;"> <apex:selectList size="1" value="{!School}" > 
                    				<apex:selectOptions value="{!schoolname}" />
          						</apex:selectList> </td> 
                <td style="width: 200px; padding-left: 100px; height: 25px;"><apex:selectList value="{!Year}" size="1"> 
                <apex:selectOptions value="{!AcademicYear}"  />
      			</apex:selectList></td>
       <td style="width: 200px; padding-left: 200px; height: 25px;"> <apex:inputText id="text-input-01" value="{!std}" list="{!standardAndSection}" html-placeholder="Enter Standard" /></td> 
 			<td><apex:commandButton value="Go!" action="{!search_student}"/></td>  
              </tr>
                 </table>
        </h1>
        <br/><br/>
     <table class="slds-table slds-table--bordered profilelisting">
        <apex:variable var="c" value="{!0}" /> 
                <thead>
                    <tr class="slds-text-heading--label">
                        <th scope="col"   width="2%"   >
                          Student Name
                      </th>
                       <th scope="col"   width="12%"   >
							{!subj1}
                    </th>    
                         <th scope="col"   width="12%"   >
							{!subj2}
                    </th> 
                         <th scope="col"   width="12%"   >
							{!subj3}
                    </th> 
   					</tr>
                </thead>
                <tbody>   
                    <apex:variable var="cnt" value="{!0}" /> 
                    <apex:repeat value="{!students}" var="stud" >
                                 <tr>
                            <td>  
                                {!stud.Student_Name__r.Student_First_Name__c}  {!stud.Student_Name__r.Student_Last_Name__c}
                            </td>
                                         <td>
                                <apex:inputText value="{!mark}" label="mark[cnt]" html-placeholder="Enter Marks" />
                            </td>   
                                         <td>
                                <apex:inputText value="{!mark}" label="mark[cnt]" html-placeholder="Enter Marks" />
                            </td>   
									    <td>
                                <apex:inputText value="{!mark}" label="mark[cnt]" html-placeholder="Enter Marks" />
                            </td>   

                           </tr>
               <apex:variable var="cnt" value="{!cnt+1}" /> 
                         </apex:repeat> 
                       </tbody>
        </table>
         <apex:commandButton action="{!save}" value="Save" />     
    </apex:form>
</apex:page>

Controller:
public class MultiStudentMarks {
	public Integer stusize {get;set;}
    	List <Student_Scores__c> StudentScores;
	 public string School {get;set;}
     public string Year {get;set;}
     public string subj1 {get;set;}
         public string subj2 {get;set;}
 		public string subj3 {get;set;}
     	public Decimal mark {get;set;}
         public Decimal mark1 {get;set;}
     public List<Student_Academic_Class__c> Students{get;set;}
	public List<String> standardAndSection {get;set;}
     public string std {get;set;}
    private Student_Academic_Class__c StudentsDetail;
  public MultiStudentMarks() {
	subj1='Tamil';
    subj2='English';
    subj3='Maths';
        standardAndSection = new List<String>();
         for(Student_Academic_Class__c Stdsec: [select id,name from Student_Academic_Class__c ])
			standardAndSection.add(Stdsec.name);
    }
       public void search_student() {
                     Students =[select id,Student_Name__r.Student_First_Name__c,Student_Name__r.Student_Last_Name__c,year__c,School_Name__c,name,(select id,Name, Subject_Name__c,First_Exam_Marks__c,Second_Exam_Marks__c, Final_Exam_Marks__c,Student_Academic_Class__c from Student_Scores__r),Student_Name__r.Name from Student_Academic_Class__c where  name=:std and year__c= :year and School_Name__c  = :School];
   					stusize=Students.size();
       }  
        public List<SelectOption> getschoolname() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('selectsch','-- Select School Name --'));
    for(Schools_Master__c c : [select ID, School_Name__c from Schools_Master__c]){
        options.add(new SelectOption(c.ID, c.School_Name__c));
    } 
    return options;
}
         public List<selectoption> getAcademicYear()
{           
    list<selectoption> options = new list<selectoption>();            
    Schema.DescribeFieldResult fieldResult = Student_Academic_Class__c.Year__c.getDescribe();
    list<schema.picklistentry> values = fieldResult.getPickListValues(); 
        options.add(new SelectOption('selectst','-- Select Academic Year --')); 
    for (Schema.PicklistEntry a : values) 
    {     
        options.add(new SelectOption(a.getLabel(), a.getValue()));
    } 
  
    return options; 
}


public PageReference save() 
{
    for (Student_Academic_Class__c stu :Students){
        for(integer i=0;i<students.size();i++){
       StudentScores.add(new Student_Scores__c(Subject_Name__c='Tamil',First_Exam_Marks__c='mark'));  
           }
        insert StudentScores;
    }
return null;
}
}

The error shown in line 54.
Subject_Name__c indicates subject column
First_Exam_Marks__c indicates Marks column in the student_score__c object.

Thanks,
 
VineetKumarVineetKumar
I suppose you must be getting a null pointer exception
Please initialise the list of StudentScores in your constructor.
StudentScores = new List<Student_Scores__c>();
Sivasankari MuthuSivasankari Muthu
Hi,
   List of list values I want to store in student_score object..
Each student contains 4 to 5 rows in that objects.How to give dynamic name into the <apex:input Text> in visualforcepage

Thanks,