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
KPGUPTAKPGUPTA 

HOW TO SAVE A RECORD IN APEX CODE?

HELLO ,

Actualy i am creating a examination setup. in that i have to save the marks of the student class wise. i have three objects student profile in which student name and student marks filed there , subject in which subjects names are there and current one is examination setup in which m fetching the student name and subject name. i want to save student marks 

 

studentname/subject      sub1                                    sub2                              sub3

studentname              inputfield(marks)        inputfield(marks)          inputfield(marks)

 

i make this thing but i got a problem while saving this 

so can any body please help me regarding tihis????

SForceDeveloperSForceDeveloper

Hi.. Can you share what is the error you are getting?

KPGUPTAKPGUPTA

I dont find any error but my record is not saving.

bob_buzzardbob_buzzard

We'll need more information to be able to help.

 

E.g. are you using standard/custom controllers?  How are you attempting to save the record?  

 

If you can post the code that would be the best option.

KPGUPTAKPGUPTA
public class ExaminationSetup 
{
  
  
    public void go()
    {
        citylist.clear();
        options.clear();
        activitylist.clear();
        stuMarks.clear();
        try
        {               
            class1 = examMarks.Class1__c;
            section = examMarks.Examination_Type__c;
            date1= examMarks.Date__c;
            
                examdetail = [Select ID, Student_Name__c FROM Examination_Setup__c];
                subjectlist =[SELECT SubjectId__r.Name,ClassId__c,Marks__c FROM SubjectClassRole__c WHERE (ClassId__c =:class1) AND (SubjectId__r.Name<>NULL)];
                if(class1!=null)
                { 
                    citylist = [Select Id,Name, Student_Name__c,Class_new__c FROM Student_Profile__c WHERE (Class_new__c=:class1) ORDER BY Student_Name__c ASC];
                    for(Student_Profile__c sku:citylist)
                    {
                        Examination_Setup__c sp1=new Examination_Setup__c();
                        sp1.Student_Name__c=sku.Name + ' ' + sku.Student_Name__c;
                        sp1.StudentID__c=sku.Name;
                        activitylist.add(sp1);
                    } 
                    for(SubjectClassRole__c subject : subjectlist)
                    {
                        Examination_Setup__c subjectdata=new Examination_Setup__c();
                        subjectdata.Subject__c=subject.SubjectId__r.Name;
                        subjectdata.Marks__c=subject.Marks__c;
                        subjectdata.Class1__c=subject.ClassId__c;
                        subjectdata.Student_Name__c=citylist[0].Student_Name__c;
                        system.debug('jawakaan de marksssssssssss' + subjectdata.Student_Name__c);
                        stuMarks.add(subjectdata);
                   }
               }
            }      
              public List<SelectOption> getactivitynames()    
              {        
                  return options;    
              }
              public List<Examination_Setup__c> getRecords() 
              {   
                  return activitylist;
              }
              public List<Examination_Setup__c> getSubjects() 
              {   
                  return stuMarks; 
              }
              public void setActivity(String activity)     
              {        
                  this.activity = activity;        
                  String section1=this.activity;
              }
              public String getActivity()     
              {        
                  return activity;    
              } 
 
        
         public PageReference save()
         {
         try  
         {  
            saveclass=act.Class1__c;
            saveexam=act.Examination_Type__c;
           
            insert stuMarks;
        }         
       
    }
        
}

 

 

 

=====================Visual Force page============================================

 

<table border="1" align="center">

 

    <tr>

       <td>

 

        </td>

        <apex:repeat value="{!Subjects}" id="repeat1" var="item">

        <td>

            <center><b><apex:outputField value="{!item.Subject__c}" id="theValue"/></b></center>

        </td>

        </apex:repeat>

    </tr>

    <apex:repeat value="{!Records}" id="repeat2" var="pitem">

    <tr>

        <td><center><b><apex:outputField value="{!pitem.Student_Name__c}" id="theValue"/></b></center></td>

 

        <apex:repeat value="{!Subjects}" id="repeat3" var="item">

        <td>

            <center><b><apex:inputField value="{!item.Marks__c}" id="theValue"/></b></center>

        </td>

         </apex:repeat>

     </tr>

      </apex:repeat>

 

 

</table>    

KPGUPTAKPGUPTA

that was my code ... i display my record in matrix form but i got a problem while saving my record..

please help me...

 

like my student name and subjects are given according to class wise i have to enter the marks of each student according to subject wise.. but my save method dont save it . it will just save name of the students and subject names but not marks detail of all students. i want to save that ...

 

bob_buzzardbob_buzzard

In your save method you are trying to insert stuMarks, but that doesn't appear to be associated with anything on your page.

 

 

KPGUPTAKPGUPTA

That what i am saying i dont know how to do this. can you please correct my code if you understand myquery.

i am sending you a second approach which is also not working..

 

 

public PageReference save()
         {
         try  
         {  
            saveclass=act.Class1__c;
            saveexam=act.Examination_Type__c;
            marks1=stuMarks.Marks__c;
            studID=act.StudentID__c;
            subjectnames=act.Subject__c;
            for(SubjectClassRole__c subject : subjectlist)
            {
                Examination_Setup__c newrec = new Examination_Setup__c(Student_Name__c=section,
                                                                        Class1__c=saveclass,
                                                                        Examination_Type__c=saveexam,
                                                                        Subject__c=subject.SubjectId__r.Name,
                                                                        StudentID__c=act.StudentID__c,
                                                                        Marks__c=subject.Marks__c);
            
            //insert stuMarks;
            insert newrec;
            
            }
            //insert stuMarks;
        }         
        catch(System.DMLException e)
        { 
            ApexPages.addMessages(e);                                      
        }                                        
        PageReference pr = new PageReference('/a0l/o');  
        pr.setRedirect(true); 
        return pr;  
    }

 

 

This is my second approach of saving the record...

If you have any other approach than please tell me i need this urgently..

 

 

bob_buzzardbob_buzzard

Where has subjectlist come from in this snippet?

 

If you want to save the marks, you'll need to insert an object that is associated with the marks input field on your page.  Looking at the code again, you have backed the Subjects list with stuMarks.  That being the case I would expect your first attempt to have worked.  Are you absolutely certain that it isn't saving anything?  E.g. have you put some debugging in there to output the result of the insert etc?

KPGUPTAKPGUPTA

It will just saveclass name of student and subjects, but i want to save the marks of the student also. which is my requirement. can u please make the code for this. 

bob_buzzardbob_buzzard

But your code should save the marks.  Are you certain that it isn't?  It might be the case that the marks are saved but not correctly associated with the student etc.

KPGUPTAKPGUPTA

thanxxx for you replies but marks are not saving i got a problem that it will save the marks of 1st row but it will not save the marks of all other rows. 

bob_buzzardbob_buzzard

I'd suggest you add debug to dump out the rows that you are attempting to insert, and then dump them out again after you have inserted.  This will show you the actual ids of the objects that were inserted.

 

If you aren't getting an error, it smacks that either you aren't inserting the rows that you think you are, or that you are inserting them but can't reach them via your relationships.