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

2 Answers 2


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.');
  • How to make it specific for NameSpace? I want child objects of my namespace only.
    – San
    Commented Jan 19, 2015 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
    Commented Jan 19, 2015 at 17:36

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
            System.debug('getChildSObject: ' + sc.getChildSObject());
            //System.debug('getField: ' + sc.getField());
            //System.debug('getRelationshipName: ' + sc.getRelationshipName());
            //System.debug('isRestrictedDelete(): ' + sc.isRestrictedDelete());

    System.debug('countDetailObjectsRelatedToMaster: '+countDetailObjectsRelatedToMaster);
  • 1
    thanks, exactly what i was looking for
    – TheShadow
    Commented Apr 24 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .