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
allMetadataList.add(clTranslation);
List<MetadataService.SaveResult> results = service.updateMetadata(allMetadataList);
for(MetadataService.SaveResult result: results){
system.debug('Insert custom label translation results success=' + result.success);
}
// ------- Not Working --------- //
}
}
}
MetadataService.Translations
?