fflib-apex-common
fflib-apex-common copied to clipboard
How to get Owner.XXX fields for custom objects? fflib_QueryFactory
Assume we have a selector on a custom object Foo__c
The following does not work:
public List<Foo__c> selectByIdWithOwner(set<ID> idSet>) {
fflib_QueryFactory fooQF = newQueryFactory()
.selectFields(new List<String>{'Owner.Alias'});
}
Result is error EXCEPTION_THROWN [112]|fflib_QueryFactory.InvalidFieldException: Invalid field 'Alias' for object 'Group'
What happens is the method selectFields in fflib_QueryFactory calls getFieldToken('Owner.Alias') and that method does a describe on OwnerId and comes back with lastSObjectType = Group.SObjectType (!?!) and a comment //if it's polymorphic doesn't matter which one we get
But it does matter as there is no field Group.Alias - only User.Alias. Since custom objects can be members of queues, the describe is returning, by chance, via getReferenceTo()[0] the Group (i.e. Queue) and not the intended User.
AFAIK, there is no way around this when using the fflib_QueryFactory and one has to resort to the legacy String.format(...) query construction (and thus you end up with a mix of styles for query construction in your code base)
One possible suggestion would be to allow for the same syntax as in polymorphic Owner formula fields
public List<Foo__c> selectByIdWithOwner(set<ID> idSet>) {
fflib_QueryFactory fooQF = newQueryFactory()
.selectFields(new List<String>{'Owner:User.Alias'});
}
and
public List<Foo__c> selectByIdWithOwner(set<ID> idSet>) {
fflib_QueryFactory fooQF = newQueryFactory();
new UsersSelector().configureQueryFactoryFields(fooQF,'Owner:User');
}
Hmmm lets see if Chris Peterson has any thoughts on this, i'll tweet him this issue.
This is a very valid complaint. I'll see if I can find time to get a PR together to improve it to allow use of any relationship.
Thanks Chris, I appreciate whatever you can do.
Eric Kintzer
On Jul 21, 2016, at 20:43, Chris Peterson [email protected] wrote:
This is a very valid complaint. I'll see if I can find time to get a PR together to improve it to allow use of any relationship.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Thanks Andy
Eric Kintzer
On Jul 21, 2016, at 19:49, Andrew Fawcett [email protected] wrote:
Hmmm lets see if Chris Peterson has any thoughts on this, i'll tweet him this issue.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Is there any available enhancement on this?