+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

Find all the objects with lookup to contact

Hello All,

Is there a way to know which objects have the lookups to contact? So far, I have found following objects in our instance

Events
Quotes
Lead 
CampaignMember
Case
ZendeskSupportTicket

Yogesh
Raj VakatiRaj Vakati
Schema.DescribeSObjectResult R = Contact.SObjectType.getDescribe();
List<Schema.ChildRelationship> C = R.getChildRelationships(); 
for(Schema.ChildRelationship rel:C){
    System.debug('Chaild Name'+rel.getChildSObject());    
    System.debug('Rel Name'+rel.getRelationshipName());        
    
}

Use the above code in execute anonymous window 
Yogesh BiyaniYogesh Biyani
Hello Raj, 

Since quite a few of our relationships are null, I am wondering if we can use dynamic SOQL to find all the relationships with NO values but I am running into quite a few of these errors.
System.ExternalObjectException: data.api.DataSourceUnsupportedQueryException: This query is not supported on the OutgoingEmail object

Is there a better way to handle such errors so that the query works where possible and skip the rest? 
 
Schema.SObjectType contactSType = Contact.sObjectType;
Schema.DescribeSObjectResult dsr = contactSType.getDescribe();
integer i=0;
for(Schema.ChildRelationship child : dsr.getChildRelationships()) {
    
    System.debug(child.getChildSObject()+'--'+child.getRelationshipName()+'--'+child.getField());
    
    if(child.getRelationshipName()!=null && 
       i!=3 && 
       i!=5 && 
       i!=6 && 
       i!=14 && 
       i!=15 &&
       i!=21 && 
       i!=28 &&
       i!=44 && 
       i!=45 &&
       i!=47)
    {
        
    
        String condition= ' Where '+child.getField()+ '!=null LIMIT 10';
        String query='select '+child.getField()+ ' from '+child.getChildSObject()+condition;
        system.debug('r'+i+'='+ Database.query(query).size());
        
       
    }
    
i++;
}