I have an Apex @AuraEnabled
method that queries a collection of root objects and also several parent/child objects all at the same time. I have programmatic filtering of the child objects (returned in separate fields), but because I am also returning the root object, the child objects "leak" to the client-side via the relationship references (the __r
fields) when the root object is serialized.
What is the cleanest way to only return the root object, given that e.g. at compile-time __r
fields are not writeable? And my reading of the SObject Class clone
method is that there is no option to discard the related objects?
PS
This:
// Don't want to leak data to the client-side through the __r fields
Root__c rootOnly = new Root__c();
Map<String, Object> m = root.getPopulatedFieldsAsMap();
for (String field : m.keySet()) {
rootOnly.put(field, m.get(field));
}
suffers the problem of errors such as "CreatedById is not editable" but I'd like to keep that information. I'm starting to think that having a second query of just the root object is the only way to do this.