fflib-apex-common icon indicating copy to clipboard operation
fflib-apex-common copied to clipboard

fflib_QueryFactory support for toLabel()?

Open dudunato opened this issue 7 years ago • 9 comments

Is there a way to use QueryFactory and get the translation for a picklist? Like:

SELECT Id, Name, toLabel(ServiceType__c)
FROM Time__c

dudunato avatar Apr 08 '17 20:04 dudunato

Anyone?

dudunato avatar Apr 13 '17 17:04 dudunato

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' ?

afawcett avatar Apr 15 '17 03:04 afawcett

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!

dudunato avatar Apr 15 '17 17:04 dudunato

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.

afawcett avatar Apr 15 '17 19:04 afawcett

Is there already a solution for this? I'm trying to get the label of a Data Category in a subquery on Knowledge Articles.

tomjansen13 avatar Jan 25 '19 15:01 tomjansen13

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... ¯_(ツ)_/¯

dudunato avatar Jan 25 '19 16:01 dudunato

Hi @dudunato Thanks for the 'solution'. I guess this is better than nothing at all!

tomjansen13 avatar Jan 28 '19 09:01 tomjansen13

I was actually working on a similar requirement and I believe you can specify an alias to the field. SObject.get('') gives you the correct answer.

Example

  1. List<SObject> records = Database.query('select id, Rating, toLabel(Rating) myAccountRating from Account LIMIT 1' );
  2. String translatedValue = records[0].get('myAccountRating');
  3. system.debug(translatedValue);

I am guessing it must be possible to add an alias to a toLabel field somewhere.

anirudhs2005 avatar Apr 09 '19 08:04 anirudhs2005

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

foxysolutions avatar Dec 26 '19 12:12 foxysolutions