Like many SFDC developers, we have SObjects which are related to other custom relationships.
So, for example, MyCustomObject__c
could have a relationship with MyCustomRelationship__c
which in Apex we might normally access with MyCustomObject__c.MyCustomRelationship__r
Of course, this isn't a one-off scenario, so we might also have MyCustomObject2__c.MyCustomRelationship2__r
, MyCustomObject3__c.MyCustomRelationship3__r
, etc.
And of course these relationships don't need to be one-to-one or so neatly organized.
But the thing is, we want a generic way to process some of these, so we'd like to make a method with a signature like:
doSomething(SObject sObj, SObjectField sObjectField)
only these aren't actually SObjectFields but rather Schema.ChildRelationships, so in actually we would need a signature like:
doSomething(SObject sObj, Schema.ChildRelationship childRelationship)
But then this still present two problems:
How can I actually get the value for Schema.ChildRelationship from the API name (e.g.
MyCustomObject__c.MyCustomRelationship__r
)?Having Schema.ChildRelationship, how I can apply it to my SObject to do something like
List<SObject> relatedSObjectList = sObj.get(childRelationship)