function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Satish PrajapatSatish Prajapat 

find relationship between two unknown object

I have 2 objects may be a standard or custom objects, I want to know which the parent or which is child object??

 
Best Answer chosen by Satish Prajapat
Glyn Anderson 3Glyn Anderson 3
Here is a method that will return true if objectA is a child of objectB.  Adapt it to your need.  Keep in mind that two sObject types might look up to each other and therefore be children of each other.  And also, an sObject type can look up to itself (hierarchy relationship).  So test both ways - don't assume that the first true test is the whole truth.  (Disclaimer: this code is untested and does not handle any error conditions.)

<pre>
static Boolean isAaChildOfB( String sObjectNameA, String sObjectNameB )
{
    for ( Schema.ChildRelationship chRel : Schema.getGlobalDescribe().get( sObjectNameB ).getDescribe().getChildRelationships() )
    {
        if ( String.valueOf( chRel.getChildSObject() ) == sObjectNameA ) return true;
    }
    return false;
}
</pre>
 

All Answers

ManojjenaManojjena
Hi Satish ,

If you want to know the  relationship between existing object ,then you can go the object fields there you will find the relationship .

Assume you have object A and Object B .then if A is parent and B is child then when you wil go the fields of object B you will find a field which is either look up or master detail .

Just like below image
User-added image
Let me know if u have any doubt .

Thanks
Manoj
Satish PrajapatSatish Prajapat
Hello Manojjena, 
I want the solution in apex programming point of view not admin point of you.
because I am writing the code where i have list of object and i want to know the relationshiip among all the object. So, for initial level how can I know the relationship between two object.?
Glyn Anderson 3Glyn Anderson 3
Here is a method that will return true if objectA is a child of objectB.  Adapt it to your need.  Keep in mind that two sObject types might look up to each other and therefore be children of each other.  And also, an sObject type can look up to itself (hierarchy relationship).  So test both ways - don't assume that the first true test is the whole truth.  (Disclaimer: this code is untested and does not handle any error conditions.)

<pre>
static Boolean isAaChildOfB( String sObjectNameA, String sObjectNameB )
{
    for ( Schema.ChildRelationship chRel : Schema.getGlobalDescribe().get( sObjectNameB ).getDescribe().getChildRelationships() )
    {
        if ( String.valueOf( chRel.getChildSObject() ) == sObjectNameA ) return true;
    }
    return false;
}
</pre>
 
This was selected as the best answer
Satish PrajapatSatish Prajapat
Schema.SObjectType AObject = Schema.getGlobalDescribe().get(s1);
        Schema.SObjectType BObject = Schema.getGlobalDescribe().get(s2);
        
        for(Schema.ChildRelationship child : AObject.getDescribe().getChildRelationships()) 
        {
            if (child.getChildSObject().getDescribe().getName() == BObject.getDescribe().getName() &&
                child.getRelationshipName() != null) 
            {
                System.debug(s1+' Object Is Parent and '+ s2 +' Object is child Object');
            }
        }

This is also working.