fflib-apex-common
fflib-apex-common copied to clipboard
fflib_QueryFactory support for toLabel()?
Is there a way to use QueryFactory and get the translation for a picklist? Like:
SELECT Id, Name, toLabel(ServiceType__c)
FROM Time__c
Anyone?
Sorry @dudunato for the delayed response. I don't see a way of doing this currently no. @capeterson and proposals on the best API design to accomplish this, maybe overload the select method with a boolean param 'toLabel' ?
Thanks for the reply @afawcett!
Wouldn't it be better if we would have the option on a field level?
Something like query.selectField('toLabel(FieldName__c)')
Or even query.selectField('FieldName__c', true)
and this boolean parameter would be a flag to add the field to a Map to be converted to toLabel() when the method toSOQL be called.
Idk, just ideas!
Yes, your second idea is the one i was suggesting actually. Though your first is more flexible, it just means the query factory cannot validate the field as its an expression being passed.
Is there already a solution for this? I'm trying to get the label of a Data Category in a subquery on Knowledge Articles.
HI @tomjansen13,
I don't think this solutions was implemented. What I ended up doing was to use replace() function after building up the whole query. Super ugly, but... ¯_(ツ)_/¯
Hi @dudunato Thanks for the 'solution'. I guess this is better than nothing at all!
I was actually working on a similar requirement and I believe you can specify an alias to the field. SObject.get('
Example
- List<SObject> records = Database.query('select id, Rating, toLabel(Rating) myAccountRating from Account LIMIT 1' );
- String translatedValue = records[0].get('myAccountRating');
- system.debug(translatedValue);
I am guessing it must be possible to add an alias to a toLabel field somewhere.
Suggested PR change to allow toLabel within the fflib framework can be found here: https://github.com/apex-enterprise-patterns/fflib-apex-common/pull/258