[SalesForce] Metadata API : How to get the translated values of picklists of the object

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?

Best Answer

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());
}
Related Topic