+ Start a Discussion
JeffStevensJeffStevens 

Display current from an AggregateResult element

I have an Aggregateresult that I'm displaying like this...

 

<apex:column headervalue="Revenue">
<apex:outputText value="${!ar['revenue']}"/>
</apex:column>

 

But the output is always displayed with only one decimal place.  How can I get it to display two decimal places?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
JeffStevensJeffStevens

I finally got it.

 

This did it..

 

                    <apex:outputtext value="{0, number, ###,###.00}">
                        <apex:param value="{!value(TEXT(ar['revenue']))}"/>
                    </apex:outputtext>

 

All Answers

aballardaballard

CAn't you use apex:outputField here and get the standard formatting?

JeffStevensJeffStevens

No - I don't think so - because I'm display a field that I built in an AggregateResult.  If I change the page to ...

 

<apex:column headervalue="Revenue">
<apex:outputField value="{!ar['revenue']}"/>
</apex:column>

 

 

I get ...

Could not resolve the entity from <apex:outputField> value binding '{!ar['revenue']}'. outputField can only be used with SObject fields. 

aballardaballard

You are right, outputField doesn't seem to work with an aggregate result.  I think it is supposed to!

Starz26Starz26

Could you do something like:

 

Play with the #,###,###.## and #,###,###.00

 

<apex:outputText value="{0,number,#,###,###.##}">
                    <apex:param value="${!ar['revenue']}"/>
                </apex:outputText>
JeffStevensJeffStevens

Thanks for the input.  Yes - I've tried a couple of those variations also.

 

I'm a little new to apex and visualforce.  Does "number" have to be defined?  For example - when I do this...

 

<apex:outputText value="{0,number,#,###,###.##}">
<apex:Param value="${!ar['revenue']}"/>
</apex:outputText>

 

I get this error...

Save error: The value attribute on <apex:outputText> is not in a valid format. It must be a positive number, and of type Number, Date, Time, or Choice. AgentContribution.page /RLinkTest/src/pages line 0 Force.com save problem

Starz26Starz26

The number in the outputtext defines what type of value is being used.

 

I suspect that since you are using an agfgregate result that it is not a number rather it is a generic sObject....

 

Why not just have a method in the class return the value of

 

(Decimal)ar.get('revenue')

 

or whatever Revenue is:

 

Public Decimal getRevenue(){

 

    return (Decimal)ar.get('revenue');

 

}

 

then just use {!Revenue} in the VF page in the previous code I provided. Adapt it as needed.

aballardaballard

looks like a bug in outputText message formatting if it does not accept a dynamic reference.  

JeffStevensJeffStevens

I know that if I comment out the the Param line - I don't get the error.

aballardaballard

Ok, I checked this out, it is not a problem with dynamic references.   Your param value IS invalid for formatting, because you included the dollar-sign (so the value is NOT a number.   And your format is invalid because it needs quotes around the format. 

 

Try the following

 

<apex:outputtext value="${0, number,'#,###,###.##'}">
<apex:param value="{!ar['revenue']}"/>
</apex:outputText>


 

JeffStevensJeffStevens

I finally got it.

 

This did it..

 

                    <apex:outputtext value="{0, number, ###,###.00}">
                        <apex:param value="{!value(TEXT(ar['revenue']))}"/>
                    </apex:outputtext>

 

This was selected as the best answer
JeffStevensJeffStevens

Thanks for all the help to everyone!