+ Start a Discussion
src0010src0010 

Sum value that is within apex:repeat

Well, I was finally able to get one of my related lists (Cases), which is part of a custom object (X360_Contract_Cycle__c), to display properly.  I am using apex:repeat to iterate and display the related list information.  So far that is working great!  Now I am trying to figure out the best solution to SUM on of the values within the related list/apex:repeat.  The value I am trying to SUM is {!cx.Case_Hours__c}.  I included a snippet of the code below.  I am wondering if I can include the logic directly on the VisualForce page or would I need to create a custom Apex class?  Any suggestions would be much appreciated as I am still learning.  Thanks! 

 

 

<apex:page standardController="X360_Contract_Cycle__c"    showHeader="false"  renderAs="pdf">
.
.
.
                
                <tr>
                    <td>Case Number</td>
                    <td>Case Subject</td>
                    <td>Case Hours</td>
                </tr>
                
                <apex:repeat var="cx" value="{!X360_Contract_Cycle__c.Cases__r}">

                    <tr>
                        <td>{!cx.CaseNumber}</td>
                        <td>{!cx.Subject}</td>
                        <td>{!cx.Case_Hours__c}</td>
                        
                        <apex:variable var="Total" value="{SUM(!cx.Case_Hours__c}"/>
                        
                        <td>{!Total}</td>
                        
                    </tr>
                
                 </apex:repeat>
 
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

You can do the following:-
   1. You can have a trigger on case on inset, delete, update event you can modify the sum of Cx_Case_Hours__c  in this trigger.
   2. Also you can use java script code to find the repeat column values and add them through  loop which will display in the html variable
   3. Or can use a separate function in your controller for the above calculation.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Ispita_NavatarIspita_Navatar

You can do the following:-
   1. You can have a trigger on case on inset, delete, update event you can modify the sum of Cx_Case_Hours__c  in this trigger.
   2. Also you can use java script code to find the repeat column values and add them through  loop which will display in the html variable
   3. Or can use a separate function in your controller for the above calculation.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
src0010src0010

 


Ispita_Navatar wrote:

You can do the following:-
   1. You can have a trigger on case on inset, delete, update event you can modify the sum of Cx_Case_Hours__c  in this trigger.
   2. Also you can use java script code to find the repeat column values and add them through  loop which will display in the html variable
   3. Or can use a separate function in your controller for the above calculation.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.


 

 

Yes, that answering my question.  Figuring out the JavaScript and/or function is another issue all together :smileywink:.  Thanks for the response. 

 

~ Sean

kxa422kxa422

Hello,

 

I haven't test it, but something like that should give you the running sum and the total on the last row.

 

<apex:page standardController="X360_Contract_Cycle__c"    showHeader="false"  renderAs="pdf"> . . .                                  
<tr>                     
<td>Case Number</td>
<td>Case Subject</td>
<td>Case Hours</td>
<td>Running sum</td>
</tr>
<apex:variable value="{!0}" var="Total"/>
<apex:repeat var="cx" value="{!X360_Contract_Cycle__c.Cases__r}">
<tr>
<td>{!cx.CaseNumber}</td>
<td>{!cx.Subject}</td>
<td>{!cx.Case_Hours__c}</td>
<apex:variable var="Total" value="{!Total + !cx.Case_Hours__c}"/>
<td>{!Total}</td>
</tr>
</apex:repeat>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>{!Total}</td>
</tr>
</apex:page>

 

venu kvenu k

pls provide me error free code of this example plss 

 

thank you 

Naďa BokovaNaďa Bokova
The snippet provided by @kxa422 is incorrect because there is an extra "!"

You also need to add a condition to handle null values because they would mess the calculation up.
 
<apex:variable value="{!0}" var="Total"/> 
<table>

<apex:repeat var="cx" value="{!X360_Contract_Cycle__c.Cases__r}">
<tr>
<td>{!cx.CaseNumber}</td>
<td>{!cx.Subject}</td>
<td>{!cx.Case_Hours__c}</td>
</tr> 
<apex:variable var="Total" value="{!Total + IF(cx.Case_Hours__c = null, 0, cx.Case_Hours__c)}"/>
</apex:repeat> 

<tr>
<td colspan="3">Total: {!Total}</td>
</tr> 

</table>