0

I have implemented a REST Api in Apex to return list of Data Categories in Salesforce. The query looks like the one below -

SELECT DataCategoryGroupName,DataCategoryName,toLabel(DataCategoryName)DCLabel,Id,ParentId,Parent.Title FROM Knowledge__DataCategorySelection 

I am able to see all categories in response. However, I do not see DCLabel in the response.

Please advise how can I return a Data Category Label in the response.

1 Answer 1

0

Aliases only work in Aggregate Result queries. You can only get either the API value or the translated value, take your pick. This is also true for functions like convertCurrency. In the query result here, if you select just the translated value, it appears in place of the normal field, which is why aliases don't work and you can't actually query them twice.

Knowledge__DataCategorySelection[] result = [SELECT DataCategoryGroupName,DataCategoryName FROM Knowledge__DataCategorySelection];

System.debug(result[0].DataCategoryName);

...

Knowledge__DataCategorySelection[] result = [SELECT DataCategoryGroupName,toLabel(DataCategoryName) FROM Knowledge__DataCategorySelection];

System.debug(result[0].DataCategoryName);

Normally in Apex code, you only care about the untranslated value, as you'd call a describe to convert between the raw value and the translated value for display to the user.

2
  • Since my goal is to send both the Api value and the translated value, is it possible to send the response in a list of wrapper? And how can I add translated value in a wrapper class?
    – user51241
    Commented May 6, 2020 at 11:13
  • @user51241 You can use describe calls to get the translated values, and you can manually do the math for conversion by querying the CurrencyConversion and/or DatedCurrencyConversion tables. This would be the ideal way to do so. As far as the wrapper, it could be something like public class Wrapper { sObject db, translated; } and you could populate this with all the data you desire.
    – sfdcfox
    Commented May 6, 2020 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .