Hello Salesforce experts!

I would like to dynamically create "CustomLabelTranslation" using Metadata API. I am able to retrieve the translated value using specific language, but not able to then set that value for different label. Please see the code below, it is working upto first debug statement.

Example, consider I have 2 labels, I would like to dynamically set translated value from myLabel_1 into myLabel_2

Label 1: Name=myLabel_1, Language='en_US', translatedValue='Hello'

Label 2: Name=myLabel_2, Language='en_US', translatedValue=Label.translatedValue

One obvious reason why this will not work is there is no "Language" tag for CustomLabelTranslation, so not sure how the system is going to understand which language is the value for.

// Read Translation
Map<String, String> labelMapping = new Map<String, String>{'SourceLabelName' => 'TargetLabelName'};
String language = 'da';
MetadataService.Translations translations =
   (MetadataService.Translations) service.readMetadata('Translations',new String[] { language }).getRecords()[0];
for(MetadataService.CustomLabelTranslation customLabelTranslation : translations.customLabels) {
    if(labelMapping.containsKey(customLabelTranslation.name)) {
        String sourceLabel = customLabelTranslation.name;
        String targetLabel = labelMapping.get(sourceLabel);
        String translatedValue = customLabelTranslation.label;
        if(String.isNotBlank(translatedValue)) {

        // -------  Working --------- //
        System.debug('Language ' + language + '. Translation for ' + sourceLabel + ' is ' + translatedValue);

        // ------- Not Working --------- //

        //Insert translated value from source to target label

        List<MetadataService.Metadata> allMetadataList = new List<MetadataService.Metadata>();
        MetadataService.CustomLabelTranslation clTranslation = new MetadataService.CustomLabelTranslation();
        clTranslation.name = targetLabel;
        clTranslation.label = translatedValue;
        //clTranslation.language = language; //There is no language attribute on CustomLabelTranslation
        List<MetadataService.SaveResult> results = service.updateMetadata(allMetadataList);
        for(MetadataService.SaveResult result: results){
            system.debug('Insert custom label translation results success=' + result.success);

        // ------- Not Working --------- //            



  • 2
    Could you please include some narrative describing what you are doing, what the expected behavior is, and what the problem is with this code?
    – David Reed
    Sep 18, 2019 at 20:38
  • Hi David, sorry hit the submit button too early. It is now updated. Sep 18, 2019 at 20:41
  • Is not that specified as a parameter to retrieve an instance of MetadataService.Translations?
    – kurunve
    Sep 18, 2019 at 20:53
  • Yes, there is parameter "customLabels" on "MetadataService.Translations" to retrieve (READ) translation for custom label. I am using that already. I would now like to set (INSERT) the translated value for a custom label. Sep 18, 2019 at 21:08

1 Answer 1


Let's say we have such a Custom Label: enter image description here

We want to add French translation for this label. Using same Apex library (apex-mdapi) code will look like this:

 public static void updateTranslation() {

    MetadataService.MetadataPort service = createService();

    // Read whole Translation for given language - in this case french
    MetadataService.Translations translations = (MetadataService.Translations) service.readMetadata('Translations',
            new String[] { 'fr_CA' }).getRecords()[0];

    //Add new custom label translation
    MetadataService.CustomLabelTranslation customLabelTranslation = new MetadataService.CustomLabelTranslation();
    customLabelTranslation.label = 'Text in French'; // new value in french
    customLabelTranslation.name = 'SomeCustomLabel'; // name of the custom label we want to translate

    //Clear custom labels and add only these ones you want to add
    translations.customLabels = new List<MetadataService.CustomLabelTranslation>();

    //Clear rest of translations
    translations.customApplications = new List<MetadataService.CustomApplicationTranslation>();
    translations.customDataTypeTranslations = new List<MetadataService.CustomDataTypeTranslation>();
    translations.customPageWebLinks = new List<MetadataService.CustomPageWebLinkTranslation>();
    translations.customTabs = new List<MetadataService.CustomTabTranslation>();
    translations.flowDefinitions = new List<MetadataService.FlowDefinitionTranslation>();
    translations.quickActions = new List<MetadataService.GlobalQuickActionTranslation>();
    translations.reportTypes = new List<MetadataService.ReportTypeTranslation>();
    translations.scontrols = new List<MetadataService.ScontrolTranslation>();

    // Update
    handleSaveResults(service.updateMetadata(new List<MetadataService.Metadata> { translations })[0]);

And the result:

enter image description here

Hope this solution will help someone !

You must log in to answer this question.

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