Search Community
- Force.com Discussion Boards
- :
- Salesforce User Discussions
- :
- Product Discussion
- :
- Reasons for choice of relationship field type (mas...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Reasons for choice of relationsh ip field type (master-de tail or lookup)?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
06-18-2008 02:57 PM
1. Use master-detail when you want the detail object to delete along with the master object.
2. Offline edition only supports master-detail, not lookup. (I think the Mobile Edition upports both though).
3. Email templates only support master-detail, but not lookup (can anyone confirm this is still true in Summer 08?).
4. Objects can only have one Master-Detail relationship field. (can anyone confirm this is still true in Summer 08?)
5. Roll up summary field on parent only works on child objects where relationship is master-detail
Re: Reasons for choice of relationsh ip field type (master-de tail or lookup)?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
05-26-2009 11:54 PM
The biggest difference is that cascading deletes are not supported when using lookups.. And you can't have multiple master detail relationships... I've found that the biggest use of master detail relationships for me is the cascading deletes... You can get around the lack of multiple master-details but you have to write code to delete children when the kids are based on lookup relationships. Since this is something that's often used, I have written a generic class and use a configuration table that mimmics the master detail cascade deletes. It works, but, you have to watch out for SOQL limits if you have a very complicated or large object structure where you want to mimmic the master detail cascading deletes.
First create a admin configurable table called sa_CascadeDeletes__c with text fields parent_object__c, child_object__c, Lookup_Field_Name__c - populate these records manually with your object relationships
Call the class from any trigger that you want to cascade (from kids, grankids, etc.) with a simple oneliner - pass in the name of the triggering object and the keyset of records being deleted.
trigger DeleteKids on Account_Kids__c (before delete) {
If (Trigger.IsDelete) {
sa_CascadeDeleter.DeleteChildren('Account_Kids__c' , trigger.oldmap.keyset());
}
}
public class sa_CascadeDeleter {
public static void DeleteChildren(String strPO, Set<Id> setDeletes) {
//PREREQ: sa_CascadeDeletes object with fields parent_object__c, child_object__c, Lookup_Field_Name__c
//USAGE:
// 1) Called from Delete Trigger, Takes the parent object name as string and a set of record ids being deleted as input parameters
// 2) Looks up the child-object(s)-to-delete in the sa_cascadedeletes table
// 3) Deletes all indicated children records, if any, returns nothing
// Create a list of child objects that are configured in sa_CascadeDeletes to cascade delete from parent object
String strQCD = 'select id, parent_object__c, child_object__c, Lookup_Field_Name__c from sa_CascadeDeletes__c where parent_object__c = \'' + strPO + '\'';
List<sa_CascadeDeletes__c> cds = Database.Query(strQCD);
system.debug(setDeletes.size());
integer i = setDeletes.size();
String strIDs = '\'';
For ( id myID :setDeletes ) {
system.debug (myID);
strIDs = strIDs + myID;
If (i != 1) {
strIDs = strIDs + '\',\''; }
i--;
}
strIDs = strIDs + '\'';
// For each child object set to cascade delete, repeat this loop
For (sa_CascadeDeletes__c cd : cds) {
system.debug(cd.child_object__c);
system.debug(cd.Lookup_Field_Name__c);
String strQCO = 'select id from ' + cd.child_object__c + ' where ' + cd.Lookup_Field_Name__c + ' in (' + strIDs + ')';
system.debug(strQCO);
List<sObject> lDeleteKids = Database.query(strQCO);
Delete lDeleteKids;
}
}
}
mehmet@ergun.net

