2

I want to get a list of all Custom Object which are master detail with Account object. how can i get it with SQQL or through Apex. Thanks

5

Not sure how I would do it with SOQL, but you could use the getDescribe() functionality within apex to get you started. I'll take a shot at something that might get you started. Since you are working with the Account object it is safe to assume that the Account is the Master object and all other objects are on the detail side of the relationship. Best of luck from here.

Schema.sObjectType parentType = MasterObject__c.sObjectType;

List<Schema.SObjectField> fieldList = obj.getDescribe().fields.getMap().values();

for (Schema.SObjectField field : fieldList) 
{

    Schema.DescribeFieldResult fieldDesc = field.getDescribe();

    if (Schema.DisplayType.ID == fieldDesc.getType() &&
        new Set<Schema.sObjectType>(fieldDesc.getReferenceTo()).contains(parentType)) {
        System.debug('Found child object. Let the magic begin.');
    }
}
| improve this answer | |
  • How to make it specific for NameSpace? I want child objects of my namespace only. – San Jan 19 '15 at 10:00
  • You can try to use the getLocalName() under the DescribeSObjectResult Class. Note that it will omit the namespace portion if the object you are on is part of the current namespace. You might have to use additional methods from the class such as well. See the documentation on the class, best of luck: salesforce.com/us/developer/docs/apexcode/Content/… – TC Sutton Jan 19 '15 at 17:36
0

Using Apex you can list which objects are both master-detail and child of Account:

//this example describes the Account sObject
    Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe(); //the master object. In this scenario it is Account
    List<Schema.ChildRelationship> C = R.getChildRelationships();
    Integer countDetailObjectsRelatedToMaster= 0; 
    for( Schema.ChildRelationship sc :c){
        if(sc.isCascadeDelete()){ //this means it is master-detail
            countDetailObjectsRelatedToMaster++;
            System.debug('getChildSObject: ' + sc.getChildSObject());
            //System.debug('getField: ' + sc.getField());
            //System.debug('getRelationshipName: ' + sc.getRelationshipName());
            //System.debug('isRestrictedDelete(): ' + sc.isRestrictedDelete());
        }

    }
    System.debug('countDetailObjectsRelatedToMaster: '+countDetailObjectsRelatedToMaster);
| improve this answer | |

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.