[SalesForce] translation workbench: picklist item not translated

Pick-list item not translated. I have a controller in an email template to translate dynamically the items in a picklist:

controller:

Savepoint sp = Database.setSavepoint();

User currentUser = [SELECT LanguageLocaleKey FROM User WHERE Id =: UserInfo.getUserId()];
currentUser.LanguageLocaleKey = languageToTranslate;
update currentUser;


Opportunity opp = [SELECT toLabel(Phase__c) FROM Opportunity WHERE Id = 'e4u8E6jt00wcgMqQope'];

Database.rollback(sp);

The code shows that I am changing the language of the current user according to the specified language, after that, I run a query on the opportunity object to get the Phase__c picklist value but the result is not translated.

What am I doing wrong?

Note that I have translated the pick-list items by using the translation workbench.

Best Answer

I ran a piece of anonymous Apex based on the code you provided. The strange thing is that it indeed does change the language correctly and even reflects this in the UserInfo class, but the toLabel() function of SOQL doesn't use the new language.

system.debug(userinfo.getLanguage());

User currentUser = [SELECT LanguageLocaleKey FROM User WHERE Id =: UserInfo.getUserId()];
currentUser.LanguageLocaleKey = 'NL_nl';
update currentUser;

system.debug(userinfo.getLanguage());

Opportunity opp = [SELECT toLabel(StageName) FROM Opportunity Limit 1];
System.debug(opp);

First time I run it (I started with English as language), the debug logs show: enter image description here

But the second time I run (so after the language is changed by the first run), it does translate the label: enter image description here

So either the SOQL engine doesn't look at the current UserInfo values, but at the values at the start of the execution context, or this is a bug.

Have you contacted Salesforce support about this?


Workaround:

What you could also do, is use the Schema Describe info to get the labels that correspond to the picklist values you query from your Opportunity record.

Schema.DescribeFieldResult field = Opportunity.Phase__c.getDescribe();
List<Schema.PicklistEntry> picklistValues = field.getPicklistValues();

Go through this list of picklist values and use getLabel() to get the translated value.

Related Topic