0

I have created a Visualforce Email template, In the table, all record will show dynamically with respected grade value. Now I want to add all grade value. How Can I proceed with that?

        <table>
            <tr>
               <th>Name</th>
               <th>Grade</th>                       
            </tr>
            <apex:repeat var="cn" value="{!relatedTo.Contact}">
            <tr>
               <td>{!cn.Name}</td> 
               <td>{!cn.Grade__c}</td>                     
            </tr>
            <tr>
                Total Grade :
            </tr>
            <td></td>
            </apex:repeat>                  
        </table>
        </body>

enter image description here

1

The official, proper way to do this is to create a Visualforce component. Visualforce-based emails can't have Apex controllers - but components can. And components can be embedded in emails. So you'd calculate the grand total in Apex, expose it as a class variable (or getter function) and simply display it.

You should have no problems searching for some samples, it's not rocket science if you've done some Apex+Visualforce in the past. My answer from a while ago could serve as a starting point although probably it's too complex for what you want to achieve.


The unofficial way to do it uses an undocumented behaviour of Visualforce <apex:variable> tag. Salesforce documentation explicitly says to not rely on it, it can break anytime. But - you could make it happen without any Apex code and that's very tempting (easier to maintain, no unit tests to write...). You've been warned.

<apex:variable var="grandTotal" value="{!0}" />
<apex:repeat var="cn" value="{!relatedTo.Contact}">
    <apex:variable var="grandTotal" value="{!grandTotal + cn.Grade__c}" />
    <tr>
        <td>{!cn.Name}</td> 
        <td>{!cn.Grade__c}</td>                     
    </tr>
</apex:repeat>
<tr>
    <td>Total Grade :</td>
    <td>{!grandTotal}</td>
</tr>

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.