I have a piece of code that allows me to update custom picklist field values:

public static TM_EComUtils.genericResponse addPicklistValuesViaMetadata(String fieldAPI, String sObjectAPI, List<String> newPicklistValues){
    MetadataService.SaveResult result;
    MetadataService.CustomField pckField;
    MetadataService.CustomValue newValue;
    TM_EComUtils.genericResponse GenericResponse; //wrapper for response

        MetadataService.MetadataPort service = new MetadataService.MetadataPort(); 
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();

        //Get custom picklist field
        pckField = (MetadataService.CustomField) service.readMetadata('CustomField', new List<String>{ (sObjectAPI + '.' + fieldAPI) }).getRecords()[0];

        //Add new picklist values
        for(String value : newPicklistValues){
            newValue = new MetadataService.CustomValue();
            newValue.fullName = value;
            newValue.default_x = false;  
        if(!Test.isRunningTest()){ result = service.updateMetadata( new List<MetadataService.Metadata>{pckField})[0]; }
            result = new MetadataService.SaveResult();
            result.success = true;

        if(result.success){ return new TM_EComUtils.genericResponse(true); }

        else if(result.success && result.errors != null && !result.errors.isEmpty()){
            TM_EComUtils.storeError(result.errors[0].message, '01', null, TM_EComUtils.class.getName(), 'addPicklistValuesViaMetadata', null);    
            GenericResponse = new TM_EComUtils.genericResponse(result.errors[0].message, '01');
    catch(Exception exc){
        TM_EComUtils.storeError(null, '02', exc, TM_EComUtils.class.getName(), 'addPicklistValuesViaMetadata', null);   
        GenericResponse = new TM_EComUtils.genericResponse(exc.getMessage(), '02');
    return GenericResponse;

And it's working fine but now I need a way to also update this new values translations. Is this possible via MetadataService? Cause I'm not finding anything useful.

Thanks in advance!


Yes. You can always take a look at the MetadataService.cls to see what types are called out there. Anything in there is useable to be read if it extends Metadata.

In this case, they have CustomObjectTranslation which contains a CustomFieldTranslation which then contains the PicklistValueTranslation you seek.

Read current object translations by passing in object name + language code as noted in documentation. Example below for an object named "Custom_Object__c" and Japanese language translations.

MetadataService.MetadataPort service = MetadataServiceExamples.createService();

MetadataService.CustomObjectTranslation  objectTranslation = (MetadataService.CustomObjectTranslation ) service.readMetadata('CustomObjectTranslation',new String[]{'Custom_Object__c-ja'}).getRecords()[0];

Loop through until you get to the PicklistValueTranslation to make updates

for(MetadataService.CustomFieldTranslation customFieldTranslation : objectTranslation.fields){
    if(customFieldTranslation.picklistValues != null){
        for(MetadataService.PicklistValueTranslation picklistValueTranslation : customFieldTranslation.picklistValues){
            //update the translation field to be the value you're seeking
            picklistValueTranslation.translation = 'New Value';

You can then pass the same custom object translation to the MetadataService to update with the new translations you changed above for a given language

List<MetadataService.SaveResult> results =
                new MetadataService.Metadata[] { objectTranslation });

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.