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

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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;
}
share|improve this answer
2  
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
    
Very good point @sfdcfox. Thanks –  sfdc_ninja Dec 12 '13 at 15:33

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>
share|improve this answer
2  
Upvote for answering the question asked. –  Shane McLaughlin Dec 13 '13 at 1:48

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.