function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael3.BrownMichael3.Brown 

Can't figure out how to display a simple SUM function on my VF Page

Hello, I need to display the SUM of Cash collected for a certain employee, but I'm having trouble figuring out how.  Since I'm using the sum function, I plan on only having 1 field come back in my SQL Statement.

 

The SQL Statement I have is: Select Sum(CashCollection) From Sales Where employeeid='220066'

 

 

I created a very simple Apex class for this. Everything compiled without error, so I'm assuming it's correct, but I am still new to SalesForce SQL

 

public class CashCollection
{
    SObject sumCash;     
        
    public SObject getSumCash()  
    {
    sumCash = [SELECT SUM(CashCollection) sum FROM Sales__c WHERE employeeid__c = '220066'];
    return sumCash;
   }
}

 

 

My Visual Force page is where I'm struggling on how to pull my sum value.  Here's the code I have, but I'm getting an unrecognizable property error: "CashCollection.sumCash not recognizable"

 

<apex:page controller="CashCollection">
    <apex:dataTable value="{!sumCash}" var="temp">  
        <apex:column >
            <apex:outputText value="${!temp.sumCash.sum}"/>
        </apex:column>
    </apex:dataTable>
</apex:page>

 If anyone could help provide insight into this, it would be great.

 

Thanks!

Mike

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This line:

 

 

<apex:outputText value="${!temp.sumCash.sum}"/>

 

 

Is the culprit I'd say.  'temp' is the returned value from the getSumCash method, and it doesn't have a property called sumCash.  However, simply changing it to:

 

 

<apex:outputText value="${!temp.sum}"/>

 

 

probably won't work either, as it is a generic sobject, and they don't have a property named sum either.

 

You should be able to use dynamic visualforce bindings for this:

 

 

<apex:outputText value="${!temp['sum']}"/>

 

 

All Answers

bob_buzzardbob_buzzard

This line:

 

 

<apex:outputText value="${!temp.sumCash.sum}"/>

 

 

Is the culprit I'd say.  'temp' is the returned value from the getSumCash method, and it doesn't have a property called sumCash.  However, simply changing it to:

 

 

<apex:outputText value="${!temp.sum}"/>

 

 

probably won't work either, as it is a generic sobject, and they don't have a property named sum either.

 

You should be able to use dynamic visualforce bindings for this:

 

 

<apex:outputText value="${!temp['sum']}"/>

 

 

This was selected as the best answer
aballardaballard

One more issue.  The method getSumCash is returning an SObject, not an array of sObjects,   So you can't use it as the value of a table interation. 

 

I don't think you need the table at all here.   You get back a single row assigned to the sobject, so just 

<apex:outputText value="{!sumcash[sum]}" /> should do it.

 

Or unless this method is used for other purposes also, why not make getSumCash return the number directly?

bob_buzzardbob_buzzard

Actually you can use a single sobject as the value for a table.

 

I agree that it's counter intuitive and there's no real need for it, but if you want to present the one item in table form with a single row then it is a viable solution.

Michael3.BrownMichael3.Brown

Thanks everyone for replying.

 

The following piece of code worked for me: <apex:outputText value="${!temp['sum']}"/> on my Visual Force page.

 

I have another quick question. Since I'm only returning one field, is it necessary to set up the <apex:dataTable> structure to display this value? I tried just using the <apex:outputText> tag alone, but it didn't seem to work.

 

Thanks!

Mike

bob_buzzardbob_buzzard

That should work okay.  You won't be able to use the temp variable though as that's your table iterator.  You should be able to apply the same dynamic technique to the sumCash property:

 

 

<apex:page controller="CashCollection">
    <apex:outputText value="{!sumCash['sum']}"/>
</apex:page>

 

 

Michael3.BrownMichael3.Brown

Great. Thanks, Bob! That line of code worked perfectly. I'm going to use this instead of creating a dataTable structure every time because it will help organize and clean up my code better.

Amit Singh1989Amit Singh1989

Thank you Bob, it also works for me...

but whatever result i am getting, is in numeric format, i want that it should be in currency format,

 

for ex.

right now i am getting result something like this

 

Amount   $3750000.07

 

it should look like  $37,50,000.07

 

How can i solve this issue???

 

Thank you!

bob_buzzardbob_buzzard

You'll need to use an outputtext and pass the value as a parameter to it,  something like:

 

<apex:outputText value="{0,number,$###,###,###,###,##0}">
          <apex:param value="{!<the value here}"/>
</apex:outputText>