6

Wanna get the translated values of picklists of my object? How can I get them in Apex.

I tried using Metadata APi but invain. Can anyone suggest me?

  • Do you need all the possible translations, or just those for the session user? If you just want them for the current user then you could retrieve the Schema.PicklistEntry list fro the field and then use getLabel() to get the transalation. There is also toLabel() that you can use in a SOQL query. – Daniel Ballinger Sep 28 '14 at 23:00
7

In the Metadata API CustomFieldTranslation has a picklistValues field of type PicklistValueTranslation[]. It has a masterLabel and translation string.

So your package.xml for the retrieve call would be something like:

<types>
    <members>*</members>
    <name>CustomObjectTranslation</name>
</types>

You will get something like:

<?xml version="1.0" encoding="UTF-8"?>
<CustomObjectTranslation xmlns="http://soap.sforce.com/2006/04/metadata">
<!-- SNIP... -->
<fields>
    <name>LeadSource</name>
    <picklistValues>
        <masterLabel>Advertisement</masterLabel>
        <translation>Anzeige</translation>
    </picklistValues>
    <picklistValues>
        <masterLabel>Customer Event</masterLabel>
        <translation>Kundenveranstaltung</translation>
    </picklistValues>

I am trying to build a custom wizard where user can load a file and save the records. I need to check if the translated picklist values are correct and then save them back into database

You can get the translated picklist values directly in Apex without having to go via the Metadata API.

List<Schema.PicklistEntry> picklistValues = 
    SomeSobject.SomePicklistField__c.getDescribe().getPicklistValues();
for (Schema.PicklistEntry pe: picklistValues) {
    system.debug('PicklistEntry Value:' + pe.getValue() + ' Translated Label:' + pe.getLabel());
}
  • I am trying to build a custom wizard where user can load a file and save the records. I need to check if the translated picklist values are correct and then save them back into database. – Charan Vuyyuru Sep 30 '14 at 12:08
  • 1
    @CharanVuyyuru You can use the Schema.PicklistEntry metadata to get the translated picklist values for the current user. – Daniel Ballinger Sep 30 '14 at 19:03
  • Thanks for your reply. If we do a Field Describe on Picklist field, will the labels of picklist values are retrieved based on User Language? – Charan Vuyyuru Sep 30 '14 at 19:08
  • @CharanVuyyuru I believe so, yes. The results to Schema.PicklistEntry.getLabel() should be for the current users language. You could test this quickly with the code sample I've given in anonymous apex. Just swap out the sObject and field names. – Daniel Ballinger Sep 30 '14 at 20:48
  • 1
    @DanielBallinger I'm trying this solution and I'm getting all the languages values in english, even if the user language is set to another language, do we have any other way to enforce this in apex to get the values from a specific language? – pfernandez Aug 30 '16 at 13:37
0

You have to include object in package.xml.

<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>CustomObject</name>
    </types>
    <types>
        <members>*</members>
        <name>CustomObjectTranslation</name>
    </types>
    <version>45.0</version>
</Package>
  • I think this answer is of limited value because to me it looks like a less detailed version of the existing answer (which was made 5 years ago). Similar answers can be fine, but I like to see something to distinguish them (a different perspective/approach, additional information, etc...) – Derek F Oct 5 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.