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
Vivek KidambiVivek Kidambi 

Master Details relationship

Class is in a Master Detail relation with student

 

Which is master object and which one is detail here?

Best Answer chosen by Admin (Salesforce Developers) 
Ashish_SFDCAshish_SFDC

Hi Vivek, 

 

As per point 3 - Student can take multiple classes, i would suggest you to go for a Lookup relation instead of Master Detail. 

You may consider creating a Junction Object for Attendance which has 2 parents Student and Class to track the attendance. 

 

See the link for junction object: https://help.salesforce.com/apex/HTViewHelpDoc?id=relationships_manytomany.htm&language=en

 

In the VF page you can customize is as per your requirement - either you have multiple look up fields or create new Student button to create or add a student to the class. 

 

Regards,

Ashish

All Answers

ericmonteericmonte

So if you created a Master detail relationship field from the Student, then master would be the Class(parent). If you created the field in the Class that master details to the student then Student would be the master (parent) Look into this:

 

http://www.salesforce.com/us/developer/docs/api/Content/relationships_among_objects.htm

Ashish_SFDCAshish_SFDC

Hi Vivek, 

 

Class and Student can be a look up as well in the actual sence - There can be number of classes and there can be number of students - which means a Student has to look up his class and a class can look up and add as many students possible. 

 

The classic example would be a Case and the Related Call tasks - where case is the Master and Tasks(Calls) are child - If a Case is deleted then there is no use of the Calls made - which means when a Case is deleted so will be the Child Records. 

 

In case of Student and Class - Students and Class can be deleted without deleting each other. 

 

We can make a Class a Master Object and  Students Child object but the child will have no value outside the parent. 

 

Reply back if there is anything specific you are looking for, 

 

Regards,

Ashish

Satish_SFDCSatish_SFDC
Ideally Class should be the master object and Student the child object.

Reason: One class can have many students but one student cannot be a part of many classes.

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
rajking6rajking6

Parent would be Class 

Child would be Student

 

Because without class we wont have a student.

so class would be parent and student would be child.

 

One depends on another, when you apply master-detail relationship if parent record is deleted the child record would be deleted. so if you delete a class record then student record would be deleted.

 

ex: you have java for a class and if they delete a class called java then we wont have any students going to that class

Vivek KidambiVivek Kidambi

Ashish,

 

This is my requirement 

 

1- Create custom objects , Class and Student.

2- Class is in a Master Detail relation with student

3- Student can take multiple classes

4- Student has a status (Attending, Dropped) for each class

5- Create a single visual force page to create a new Class record.

6- In the same page, create a section on which a user can add students to the

class via the Add button.

7- Only partial refresh in the VF page to reflect student records added.

8- Top Save button save the class and students records in the section.

9- Save button in the bottom section only saves students records

10- New button creates a new class

11- Add button adds a single student attendance record on the page, on which

the user can fill in the required values (Student, status)

12- To delete an attendance, user should select at least one record.

13- User can select multiple records to delete

 

If you look at #9  - save student records alone - This can be achieved only if student is a master and class is in detail side. 

 

Let me know your thoughts. 

 

 

Ashish_SFDCAshish_SFDC

Hi Vivek, 

 

As per point 3 - Student can take multiple classes, i would suggest you to go for a Lookup relation instead of Master Detail. 

You may consider creating a Junction Object for Attendance which has 2 parents Student and Class to track the attendance. 

 

See the link for junction object: https://help.salesforce.com/apex/HTViewHelpDoc?id=relationships_manytomany.htm&language=en

 

In the VF page you can customize is as per your requirement - either you have multiple look up fields or create new Student button to create or add a student to the class. 

 

Regards,

Ashish

This was selected as the best answer
Vivek KidambiVivek Kidambi

Thanks Ashish. I was thinking on the same lines too. Here is the page i developed using attendance as junction box

 

<--VF Page-->
<apex:page controller="classAttendance"> <apex:form > <style> .centerTable { margin: 0px auto; bottom: 0px; } </style> <apex:pageBlock > <apex:pagemessages id="msg"/> <div style="text-align:center;"> <apex:commandButton value="New" action="{!newClass}"/> <apex:commandButton value="Save" action="{!saveclass}"/> <apex:commandButton value="Delete" action="{!deleteClass}"/> </div> <apex:pageblockSection > <apex:pageblocksectionItem > <apex:outputLabel value="Class Name" for="classname"/> <apex:inputField id="classname" value="{!stuClass.Name}"/> </apex:pageblocksectionItem> </apex:pageblockSection> <div style="text-align:center;"> <apex:commandButton value="Add" action="{!addrow}"/> <apex:commandButton value="Save" action="{!save}" reRender="studSection,msg"/> <apex:commandButton value="Delete" action="{!deletes}"/> </div> <apex:outputPanel id="studSection"> <table class="centerTable"> <tr><td> <td align="center"><b>Select</b> <table> <apex:repeat value="{!attend}" var="c"> <tr><td height="25"> <apex:inputField value="{!c.isChecked__c}"/> </td></tr> </apex:repeat> </table> </td> <td align="center"><b>Student Name</b> <table> <apex:repeat value="{!stud}" var="a"> <tr><td> <apex:inputField id="studname" value="{!a.name}"/> </td></tr> </apex:repeat> </table> </td> <td align="center"><b>Status</b> <table> <apex:repeat value="{!attend}" var="b"> <tr><td> <apex:inputField id="attendstatus" value="{!b.Status__c}"/> </td></tr> </apex:repeat> </table> </td> </td></tr> </table> </apex:outputPanel> </apex:pageBlock> </apex:form> </apex:page>

 

//Apex class
public class classAttendance { //Declarations public class__c stuClass {get;set;} public List<Student__c> stud {get; set;} public List<attendance__c> attend {get;set;} public List<attendance__c> tempA; public List<student__C> tempS; public class__C tempC; public classAttendance(){ stuClass = new class__c(); stud= new List<student__c>(); stud.add(new Student__c()); attend = new List<attendance__c>(); attend.add(new attendance__C()); } // Method to implement the functionality of bottom Save button public PageReference save() { boolean flag=false; for(Integer k = 0; k < stud.size(); k++){ ID st=stud[k].ID; if(st!=null){ flag=true; } else{ flag=false; } } if(flag==true) { upsert stud; } if (flag==false){ ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Enter student details before Save')); } return null; } // Method to implement the functionality of bottom Add button public PageReference addrow() { stud.add(new student__C()); attend.add(new attendance__C()); return null; } // Method to implement the functionality of bottom Add button public PageReference deletes() { Integer Count=0; for(Integer j = 0; j < attend.size(); j++){ if(attend.get(j).isChecked__c== true){ count+=1; } } if (Count>0){ for(Integer j = 0; j < attend.size(); j++){ if(attend.get(j).isChecked__c== true){ attend.remove(j); stud.remove(j); j--; } } for(Integer k = 0; k < stud.size(); k++){ if(attend.get(k).isChecked__c== true){ ID at=attend[k].ID; List<attendance__c> tempA = [SELECT Id FROM Attendance__c WHERE ID=: at]; } } if (tempA!= null){ delete tempA; } } else { ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Please select atleast one record to delete')); } return null; } // Method to implement the functionality of Top New button public PageReference newClass() { PageReference newocp = new PageReference('/apex/classAttendance'); newocp.setRedirect(true); return newocp; } // Method to implement the functionality of Top Delete button public PageReference deleteClass() { boolean flag=false; ID cl=stuClass.ID; if (cl!=null){ tempC = [SELECT Id FROM Class__c WHERE ID=: cl]; } else{ flag=true; } for(Integer k = 0; k < stud.size(); k++){ ID st=stud[k].ID; if(st!=null){ List<attendance__C> tempS = [SELECT Id FROM attendance__c WHERE ID=: st]; } else{ flag=true; } } if (tempS!= null){ delete tempS; } if (tempC!= null){ delete tempC; } if (flag==true){ ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Cannot delete class until its saved')); return null; } else{ PageReference newocp = new PageReference('/apex/classAttendance'); newocp.setRedirect(true); return newocp; } } // Method to implement the functionality of Top Save button public PageReference saveclass() { boolean flag = false; boolean flagA = false; ID cl=stuClass.ID; if (cl!=null){ upsert stuClass; } for(Integer k = 0; k < stud.size(); k++){ ID st=stud[k].ID; if(st!=null){ flag=true; } else{ flag=false; } } if (flag==true){ upsert stud; } for(Integer j = 0; j < stud.size(); j++){ ID st=stud[j].ID; if (st!=null && cl!=null){ flagA=true; } else{ flagA=false; } attend.get(j).student__c = st; attend.get(j).class__c=cl; } if (flagA==true){ upsert attend; } if (flagA==false || flag==false){ ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Enter Class Name and Student details before Save')); } return null; } }

 

 

Ashish_SFDCAshish_SFDC
Hi Vivek, Thats great, seems to be an ideal solution. Add a post if you have any further issues. Regards, Ashish