[SalesForce] Create both child and parent object on a single Visualforce Page

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?

Appreciated!

Best Answer

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.

Example.

     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;
Related Topic