0

I need to store the result of the outputfield in a visualforce variable. I have an object that has a picklist value that is translated into several languages(translation workbench). When the picklist field is output using the apex:outputfield tag, the picklist value gets translated into the language that is set in the visualforce page, something like below:

 <apex:outputField value="{!Opportunity.Phase__c}" lang="es"/>

My issue is that all the picklist values on the field Phase__c have a weird prefix, for example, d.phase1, g.phase1, h.phase1. My idea is to keep the result of the apex:outputfield in a apex:variable and then using the SUBSTITUTE function to get rid of the weird prefix. I cannot use javascript because everything is done in an email template.

I tried the following but does not work:

<apex:variable var="phase" value='<apex:outputField value="{!Opportunity.Phase__c}" lang="es"/>' />

How can I accomplish this?

4
  • in salesforce picklist values is automatically translate. also for output field i got same picklist values as per selected on opportunity record no prefix showing on my side. can you please share your full code and database records of picklist screen. so i can help you more. <p> <apex:page standardController="Opportunity"> <apex:pageBlock > <apex:outputField value="{!Opportunity.Status__c}" lang="es"/> </apex:pageBlock> </apex:page> </p> Commented Nov 13, 2017 at 9:52
  • 2
    did you try using <apex:variable var="phase" value="{!Opportunity.Phase__c}/> ? Commented Nov 19, 2017 at 5:19
  • Have you tried using outputText instead of outputField? OutputText has the lang attribute and allows you to use formula's in the value tag. You should than be-able to use the substitution trick like you wanted to. Unfortunately, I don't have the translation workbench set up so I can't test it.
    – Kasper
    Commented Nov 19, 2017 at 7:24
  • I need to use apex:outputField because it translates the picklist value. Commented Nov 22, 2017 at 12:45

1 Answer 1

0

I had this problem once and came up with this nasty solution:

TranslateValueController:

public class TranslateValueController {

    public String objectName {get; set;}
    public String objectId {get; set;}
    public String fieldName {get; set;}
    public String lang {get; set;}

    public SObject obj {get; private set;}
    public Object fieldValue {get; private set;}

    public TranslateValueController() {
        objectId = ApexPages.currentPage().getParameters().get('objectId');
        objectName = ApexPages.currentPage().getParameters().get('objectName');
        fieldName = ApexPages.currentPage().getParameters().get('fieldName');
        lang = ApexPages.currentPage().getParameters().get('lang');

        String query = 'SELECT '+ fieldName + ' FROM ' + objectName + ' WHERE ID = \'' + objectId + '\'';
        obj = Database.query(query)[0];

        fieldValue = obj.get(fieldName);
    }   
}

TranslateValuePage:

<apex:page controller="TranslateValueController" 
           language = "{!lang}"  
           showHeader="false" 
           sidebar="false"      
           standardStylesheets="false">

    <apex:outputField value="{!obj[fieldName]}"/>

</apex:page>

ValueTranslator:

public class ValueTranslator {

    public static String translate(String objectName, String objectId, String fieldName, String lang){
        Pagereference r = new PageReference('/apex/TranslateValuePage?objectName='+ objectName +'&objectId=' + objectId + '&fieldName=' + fieldName + '&lang=' + lang);
        String pageContent = r.getContent().toString();

        Integer spanStart = pageContent.indexOf('<span');
        Integer spanEnd = pageContent.indexOf('</span>') + '</span>'.length();

        String span = pageContent.substring(spanStart, spanEnd);

        Dom.Document doc = new Dom.Document();
        doc.load(span);
        return doc.getRootElement().getText();
    }
}

You can get the translated value in your controller by invoking:

ValueTranslator.translate('Opportunity', 'e4u8E6jt00wcgMqQope', 'Phase__c', 'es');

And then use it in your VF page.

1
  • This solution will not work because, as mentioned, it is required to get the apex:outputfield in an email template, in your solution is used PageReference which is a callout. Salesforce does not allow a call-out inside a call-out. Thank you for trying anyways. Commented Nov 23, 2017 at 12:24

You must log in to answer this question.

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