I'm constructing a dynamic query to get some fields from the database. I use a root object for that purpose and branch out from it.
My_Custom_Object__c -> Account__r.Id
So if I want to get an Account Id, I'll go through my root object to the Id:
queryString = 'Select c.Account__r.Id from My_Custom_Object__c where some condition';
I can then dynamically query to get linked fields:
My_Custom_Object__c root = Database.query(queryString)
Once I get that object back, I'd like to extract the fields. In other words I would like to say something like:
String Id = root.get('Account__r.Id');
Unfortunately, it seems that
get works only for the first level (fields, not fields in related objects). I though about recursively working through the tree with gets or converting the root to JSON and extracting the required fields from there. Both approaches seem cumbersome or error-prone.
What would be a good approach to extract the fields of related objects by their name from the root object?