Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm getting some errors when I try to concatenate two variables value in apex:outfield tag. For example, if I have two variables a1 and a2, how can I get this concantenation: value="{!a1} {!a2}"> ?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I think you can't concatenate many variables or even use variables with an apex:outputField. This visualforce tag should be used with sObject fields only!

As workaround you can use apex:outputText instead where you can do concatenation like this:

<apex:outputText value="{!a1} {!a2}" />
<apex:outputText value="{!a1 + ' ' + a2}" />

Or if using multiple sObject fields you will need to use multiple apex:outputField tags, for example in a page block table:

<apex:pageBlockSectionItem>
    <apex:outputLabel value="My Label" />
    <apex:outputPanel>
        <apex:outputField value="{!object.Field1__c}" />
        <apex:outputField value="{!object.Field2__c}" />
    </apex:outputPanel>
</apex:pageBlockSectionItem>
share|improve this answer

If it were not for the fact that it is an <apex:outputField>, you could use

{!a1 + a2}

You can only have one {!...} part in an attribute, but you can include a complete calculation within that. E.g. {!a1+5} or {!a1+a2+'moreText'} is usually perfectly valid (given the types match, of course).

Now, in an outputField {!..} part, you can only reference one field. So what you could do is make a formula field 'concatField', concatenate your values in there, and then display them using

{!concatField}
share|improve this answer
    
Mmm..so if I have two variables, Object1.a1 and Object1.a2 , what is the right form ? –  Tonino Tripodi Jun 26 at 12:46
    
Sorry, I didn't initially see the outputField part. Updated the answer: I would suggest using a formula field if you want to keep it to one outputField. –  Willem Mulder Jun 26 at 12:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.