1

I would like to calculate two variables in Visualforce for rows in two different tables and show the calculation in a column on one of those tables. For example, I want {!percent_of_inspections} to work, but it does not. Is something like this possible? What am I doing wrong?

<apex:pageBlockTable value="{!inspections}" var="r">
            <apex:column headerValue="Home Inspections" value=" {!r['HomeInspections']}"/>
            <apex:column headerValue="Dollars" value="${!r['Dollars']}"/>
            <apex:column headerValue="Plus $" value="${!r['plus_dollars']}"/>
            <apex:column headerValue="Premium $" value="${!r['premium_dollars']}"/>
            <apex:column headerValue="Prestige $" value="${!r['prestige_dollars']}"/>
            <apex:column headerValue="Plus #" value=" {!r['plus_count']}"/>
            <apex:column headerValue="Premium #" value=" {!r['premium_count']}"/>
            <apex:column headerValue="Prestige #" value=" {!r['prestige_count']}"/>
            <apex:column headerValue="Upgrade Rate" value=" {!UpgradeRate}%"/>
            <apex:column headerValue="Added Services" value=" {!r['addedservices']}"/>
        </apex:pageBlockTable><br/> 
        <h1>Added Services</h1><br/><br/>
        <apex:pageBlockTable value="{!AddedServiceResults}" var="r">
            <apex:column headerValue="Name" value=" {!r['a']}"/>
            <apex:column headerValue="Total" value=" {!r['Total']}"/>
            <apex:column headerValue="Dollars" value="${!r['Dollars']}"/>
            <apex:variable value="{!inspections['Total']}" var="insptotal"/>
            <apex:variable value="{!r['Total']/insptotal}" var="percent_of_inspections"/>
            <apex:column headerValue="Percent of Inspections" value="{!percent_of_inspections}"/>
        </apex:pageBlockTable><br/>

1 Answer 1

1

If I were you, I'd suggest calculating the values in the controller, because what you're trying to do isn't supported and might not work in the future at all. However, your main problem is that only stuff inside an apex:column will work at all, and that includes your variables.

For now, at least, the following should work:

<!-- sum the total value, before page block table -->
<apex:variable value="{!0}" var="insptotal"/>
<apex:repeat value="{!inspections}" var="inspidx">
  <apex:variable value="{!insptotal + inspidx['Total']}" />
</apex:repeat>

...

<apex:column headerValue="Percent of Inspections">
    {!r['Total']/insptotal}
</apex:column>

However, as I've been told, this may or may not work in the future, so your best bet is to calculate this data and put it all in a wrapper class instead.

7
  • Thank you. I tried this and received the error: Incorrect parameter type for subscript. Expected Number, received Text
    – Sebastian
    Sep 19, 2018 at 1:08
  • @Sebastian Yeah, just realized. One sec...
    – sfdcfox
    Sep 19, 2018 at 1:14
  • @Sebastian I realized what you were trying to do; you need to sum the grand total before you try to give a percent for each row. That involves an extra variable loop at the beginning, but then you don't need the second variable at the end.
    – sfdcfox
    Sep 19, 2018 at 1:20
  • I got this error message now: Unknown property 'AggregateResult.Total' Error is in expression '{!insptotal + inspidx['Total']}' in component <apex:variable> in page franchisemetrics Error evaluating dynamic reference 'Total'
    – Sebastian
    Sep 19, 2018 at 1:29
  • @Sebastian Hmm. Sorry, I'm all out of ideas. Doing this in Visualforce isn't recommended anyways. Just calculate the value in Apex Code and expose it as a value.
    – sfdcfox
    Sep 19, 2018 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .