I have a relationship like this:

Class Student Lookup on Student

Contact Student Master-Detail on Student

I want to create a visualforce page to create new Class and on the same page add Student to the class.

I created a controller extension of Class's controller.

I am now stuck on adding student for the class.

I have several requirements here:

1.I want to add a lookup field to add student to the class. (However, I'm on the parent object and how can I add this lookup field?)

2.I want to be able to create new student on the page. Is there a way to do this?

3.I want to be able to add students by importing a file containing student ids.
Is there a way to do this?



You are not able to add students as you are using a master detail relationship on student with contact. So for adding a student u should also add the contact id in the master detail relationship of contact.


     Class__C cl = new Class__c();   
     Student__C std = new Student__c();  
     Contact con = new Contact();

       //Add all the class information then insert the class

        insert cl;

       //Add all the information of contact and create a new contact 

         insert con;

        //Add all the information of Student and then....

         std.contact = con.id;
         std.class__c = cl.id;

         insert std;

It's worth spending some time with Visualforce in Practice. The book has a whole section on creating "wizards" in Visualforce.

The gist of the situation is that you're going to want to pass a few page references around to step your user through the process of creating a class then adding students.


Hi Checkout this blog post for a solution on parent and child object creation:


I think this will help your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.