5

I'm trying to do some conditional formatting for the Opportunity Amount field on a VisualForce page that just displays the currently set amount. There is a chance that the amount can be negative (due to carry over credits) and I want to be able to display the Amount in parentheses if it's negative.

I think I'm missing something basic about how to reference an object's fields, but I haven't been able to Google-up an example that could help me with this.

Here's my VF code:

<apex:outputText value="Opportunity Amount"></apex:outputText>
<apex:outputText value="{!IF(opportunity.Amount < 0, '$(' + opportunity.Amount + ')', '$' + opportunity.Amount)}"></apex:outputText>

I know I'm probably missing something very basic here, but would really appreciate some help.

Thank you!

6

In general, I would suggest to keep the logic at the controller layer, as opposed to the view layer.

I would do something like this

On VF

<apex:outputText value="{!myOppAmount}"/>

In Controller. (Assuming you have an opportunity property already)

public string getMyOppAmmount(){
    string s = (this.Opp.Amount > 0) ? '$' + this.Opp.Amount : '$(' + this.Opp.Amount + ')';
    return s;
}
  • 3
    Both answers are practical, but this one has one important advantage: expressions are evaluated and take up view state, while strings such as this take up less view state. – sfdcfox Dec 12 '13 at 15:31
6

Please make sure that you convert the number, opportunity.amount, to string using the TEXT() function so that the concatenation can be done.

<apex:outputText value="{!IF(opportunity.Amount < 0, '$(' + TEXT(opportunity.Amount) + ')', '$' + TEXT(opportunity.Amount))}"></apex:outputText>
  • 3
    Upvote for answering the question asked. – Shane McLaughlin Dec 13 '13 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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