Salesforce.com has two different field types for creating a relationship between objects, a master-detail or look-up. The latter is not enforced so it can be left blank when the record is created. Also if the parent record is deleted the child is not, opening the door for orphan records.
Master-detail is enforced, which means when the detail record is created the id of the master record must be supplied. This allows certain things to happen; one is that you can use the rollup functionality in the master record to summarize a field in the related detail records without using SOQL. Also if you delete the master, all the related detail records will disappear. This is good news for keep your data clean and avoid orphans, but is a consideration if there was other information in those records which you want to keep.
Both these allow for a many-to-one relationship between the objects, but there are many times you need a many-to-many relationship. Many-to-many relationships can be exemplified by using a NASCAR race series. We will want to be able to have three different objects inside Salesforce for the user to control, a car, a track and garage for the car at each track. Cars and Tracks would have the many-to-many relationship as a car races on many tracks, and a track would have many cars on it at one time.
First is the list of cars which are in the series. Here we want to the car number, team name, driver, frame manufacturer, and major sponsor. Once we understand how to make the many-to-many relationships work you will see this list and many of them should be set up as enforced relationships.
Secondly, the race tracks where the series holds its races at. Some of the information captured here might be track name, size, seating capacity, and location.
Junction objects is what Salesforce names an object which has more than one master-detail relationship where the object is the child of each. Here an object called Garage is created and has a field for the Track and a field for the Car. Also it could hold information like Stall Number to help identify it. In this example the garages themselves are not permanent structures but assembled as needed by the staff at the track.
With these objects in place I can now add a related list to Car and to Track for the Garage. The real use is though instead of just showing the Id fields of each object which is all that is stored in Garage, I can display any object in the other parent, so on a Car’s related list for Garage I can display the Track Name and size. Also for the Track I can display the Car manufacturer and major sponsor and team name next to the stall number.
These items will work if I want to list all the cars and the tracks, but with NASCAR we will want to display the actual events. After all NASCAR without races is not much more than a giant tailgate party. Next time we will show how these relationships work and how to decide if you need a master-detail or if look-up is the winning move.
About the Author
Clayton Bruckert is a Technical Architect for Spyrel. He is currently located in New York, and has over 15 years experience in technology.