How to obtain child records for a given parent record and vice versa?

I want to create an Apex function that takes a recordId of unknown entity type, identifies the entity type and subsequently returns all child records (name, id) of the given record. In addition, the function should also return all the parents of the given record (name, id). Eg: for recordId x (of entity type Account which is unknown) It should return something like: Type: Account Children Contacts (type: look up/master detail) Contact 1 Contact 2 Contact 3 . Opportunities (type: look up/master detail) Opp 1 Opp 2 Opp 3 . Parents Similar to above along with type (look up or master detail) I'm clueless as to how to go about this. Any help is much appreciated.

darthcoderUS asked May 18, 2022 at 6:46 darthcoderUS darthcoderUS 45 9 9 bronze badges

1 Answer 1

Getting the object name from an Id is easy.
Getting the parent and child records is harder (and may be impossible).

The Id class provides the getSObjectType() method, which gives you an SObjectType instance. From there, you can get the describe information, and then get the object name.

Schema.DescribeSObjectResult sobjDescribe = myId.getSObjectType().getDescribe(); System.debug('object name = ' + sobjDescribe.getName()); 

The DescribeSObjectResult class has other methods/properties that can then help you get the parent and child relationships.

For child records, .getChildRelationships() should be what you're looking for. You'd iterate over those results and be interested in calling .getRelationshipName() . Be aware that you can only include 20 child subqueries in a single query (documentation).

For the parent records, you'd need to go through each individual field and check to see if it's a relationship field. You'd then extract the relationship name (the label will be something like AccountId or My_Lookup__c , the relationship name would be Account and My_Lookup__r respectively). It's the relationship name that you'd be using in your query.

for(Schema.SObjectField sof :Schema.SObjectType.Opportunity.fields.getMap().values()) < DescribeFieldResult dfr = sof.getDescribe(); if(dfr.getType() == Schema.DisplayType.REFERENCE)< system.debug(dfr.getRelationshipName()); >> 

There are some occasions where you'd need to put some more work in, such as when a single relationship field can point to one of multiple parent record types, but the above should get the job done in most situations.

A single query can have up to 55 child-to-parent relationships (same documentation).

Building the query for this is not going to be fun, but some tips: