7

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}"> ?

2 Answers 2

10

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>
2

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}
2
  • Mmm..so if I have two variables, Object1.a1 and Object1.a2 , what is the right form ?
    – user9959
    Jun 26, 2014 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. Jun 26, 2014 at 12:53

You must log in to answer this question.