Skip to main content
added 455 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

If you want all the child metadata, you can loop through the getChildRelationships results like so:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
{
    String relationshipName = relation.getRelationshipName();
    // the above value is what you would use in a SOQL sub-query
    // e.g. SELECT Id, (SELECT Id FROM Children) FROM MyObject

    SObjectType childType = relation.getChildSObject();
    // the above value is the token for the child object

    SObjectField lookupField = relation.getField();
    // the above value is the token for the lookup field on the child object
}

If you want to get the child data, you'll need to build a dynamic query using the above results from the getRelationshipName method.

Building dynamic queries in an elegant way is perhaps beyond the scope of this question. Below is a simple example of what it might look like.

List<String> subqueries = new List<String>();
for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
    subqueries.add('(SELECT Id FROM ' + relation.getRelationshipName() + ')');
String query = 'SELECT ' + String.join(subQueries, ',') + ' FROM Opportunity';

If you want all the child metadata, you can loop through the getChildRelationships results like so:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
{
    String relationshipName = relation.getRelationshipName();
    // the above value is what you would use in a SOQL sub-query
    // e.g. SELECT Id, (SELECT Id FROM Children) FROM MyObject

    SObjectType childType = relation.getChildSObject();
    // the above value is the token for the child object

    SObjectField lookupField = relation.getField();
    // the above value is the token for the lookup field on the child object
}

If you want to get the child data, you'll need to build a dynamic query using the above results from the getRelationshipName method.

If you want all the child metadata, you can loop through the getChildRelationships results like so:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
{
    String relationshipName = relation.getRelationshipName();
    // the above value is what you would use in a SOQL sub-query
    // e.g. SELECT Id, (SELECT Id FROM Children) FROM MyObject

    SObjectType childType = relation.getChildSObject();
    // the above value is the token for the child object

    SObjectField lookupField = relation.getField();
    // the above value is the token for the lookup field on the child object
}

If you want to get the child data, you'll need to build a dynamic query using the above results from the getRelationshipName method.

Building dynamic queries in an elegant way is perhaps beyond the scope of this question. Below is a simple example of what it might look like.

List<String> subqueries = new List<String>();
for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
    subqueries.add('(SELECT Id FROM ' + relation.getRelationshipName() + ')');
String query = 'SELECT ' + String.join(subQueries, ',') + ' FROM Opportunity';
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

If you want all the child metadata, you can loop through the getChildRelationships results like so:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
{
    String relationshipName = relation.getRelationshipName();
    // the above value is what you would use in a SOQL sub-query
    // e.g. SELECT Id, (SELECT Id FROM Children) FROM MyObject

    SObjectType childType = relation.getChildSObject();
    // the above value is the token for the child object

    SObjectField lookupField = relation.getField();
    // the above value is the token for the lookup field on the child object
}

If you want to get the child data, you'll need to build a dynamic query using the above results from the getRelationshipName method.