Wednesday 26 December 2012

Getting Childrelationships from an object using dynamic Apex


If an sObject is a parent object, you can access the child relationship as well as the child sObject using the ChildRelationship object methods.
A ChildRelationship object is returned from the sObject describe result using the getChildRelationship method. For example:

Schema.DescribeSObjectResult   describeresult = Account.SObjectType.getDescribe();//it gives Account  object properties or describe results
List<Schema.ChildRelationship>   lstchildrelationships =describeresult.getChildRelationships(); //It gives you all the childrelationships associated with the account.
To get relationship names from the above list
for(Schema.ChildRelationship relname:lstchildrelationships){
      System.debug('Relationshipname:'+relname.getrelationshipname();
}
Limitation:
You can only use 100getChildRelationships method calls per Apex request.

The following table describes the methods available as part of the ChildRelationship object. None of the methods take an argument.
NameData TypeDescription
getChildSObjectSchema.SObjectTypeReturns the token of the child sObject on which there is a foreign key back to the parent sObject.
getFieldSchema.SObjectFieldReturns the token of the field that has a foreign key back to the parent sObject.
getRelationshipNameStringReturns the name of the relationship.
isCascadeDeleteBooleanReturns true if the child object is deleted when the parent object is deleted, false otherwise.
isDeprecatedAndHiddenBooleanReserved for future use.
isRestrictedDeleteBooleanReturns true if the parent object can't be deleted because it is referenced by a child object, false otherwise.



No comments:

Post a Comment

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...