function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lnryanlnryan 

Dynamic Apex not recognizing child records retrieved with Database.query(str)

We have a number of objects which all have child records designating specialized Responsibilities for working with the given object. The Responsibilities are defined in a centralized table which includes a picklist for designating for which objects a given responsibility should also be the record owner. The idea here is to ensure sychronization of Responsibility and Owner assignments. For all of these objects, the interaction between parent object and responsibility children is the same and the child responsibilty objects have been set up identically, including the child relationship names, specifically so that we can leverage dynamic apex and centralize the underlying code controlling the behaviors of these records.

 

The problem is when we try to retrieve the child records from a query of the parent records + related child responsibilities (using parents[0].getSObject('Responsibilities__r') )we get an error that the relationship is not recognized. Since we're dealing with multiple potential parent object types, we've constructed the query as a string and retrieved the records using Database.query(str). I've tested the query in the schema browser and it retrieves the children there..

 

My code's below. any guidance would be much appreciated. The documentation only had examples for retrieving parent records from children not the other way around, so I'm at a loss:

 

public static void enforceOwnershiponResponsibility(sObject[] parents){ String sObjectName = ''+parents[0].getSObjectType(); //we only need to get one result because we are only using it to deduce which child responsibility object we're working with String q = 'Select Id, OwnerId, (Select Id, User__c From Responsibilities__r Where Responsibility__r.Ownership__c includes (:sObjectName)) FROM '+sObjectName+' WHERE ActiveResponsibilities__c>0 Limit 1'; sObject[] parentWithRespie = Database.Query(q); SET<ID> pids = new SET<ID>{}; String respieName = ''+parentWithRespie[0].getSObject('Responsibilities__r').getSObjectType(); //collect ids of responsibilities to change for (sObject p: parents){ pids.add(p.Id); } //retrieve respies to update String parentField = respieName.replace('Responsibility__c',''); sObject[] respies = Database.Query('Select ID, OwnerID '+parentField+'__r.OwnerId From '+respieName+' Where '+parentField+'__r.Id IN(:pids) AND Responsibility__r.Ownership__c includes (:sObjectName))'); for (sObject r: respies){ r.put('User__c',r.getSObject(parentField+'__r').get('OwnerId')); } update respies; }

 

XactiumBenXactiumBen

If you want to retrieve child records from a parent you need to use parents[0].getSObjects('Responsibilities__r');

 

The getSObject method is for retrieving fields from a parent.