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
Preeti Khanna 10Preeti Khanna 10 

To calculate sum of fields and display on the visualforce page

I have created 3 fields say Amount1,Amount2 and Amount3 in an object.User will input the same on visualforce page and its sum should be displayed in total field on the visualforce page.
Best Answer chosen by Preeti Khanna 10
Mahesh DMahesh D
Hi Preeti,

Please find the modified code:
 
<!--  Page: -->
<apex:page standardController="Account" extensions="AccountSumExtController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount1__c}">
                    <apex:actionSupport event="onchange" action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount2__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount3__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:commandButton value="Calculate" action="{!calculate}"/> <br/><br/>
        
        <apex:outputLabel id="oId">Sum of Above 3 fields is: {!total}</apex:outputLabel>
        
    </apex:form>
</apex:page>

Controller is same and no change.

I also tested the above code and it looks good.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Preeti,

Please find the below code:

VF Page:

 
<!--  Page: -->
<apex:page standardController="Account" extensions="AccountSumExtController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Amount1__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Amount2__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Amount3__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:commandButton value="Calculate" action="{!calculate}"/> <br/><br/>
        
        <apex:outputLabel>Sum of Above 3 fields is: {!total}</apex:outputLabel>
        
    </apex:form>
</apex:page>

Controller Extension:
 
public class AccountSumExtController {
    public Account acc {get; set;}
    public Integer total {get; set;}
    public AccountSumExtController(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
        
        if(acc != null && acc.Id != null) {
            acc = [Select Id, Name, Amount1__c, Amount2__c, Amount3__c from Account where Id =: acc.Id];
        }
    }
    
    public PageReference calculate() {
        total = (acc.Amount1__c != null ? Integer.valueOf(acc.Amount1__c): 0) +
                (acc.Amount2__c != null ? Integer.valueOf(acc.Amount2__c): 0) +
                (acc.Amount3__c != null ? Integer.valueOf(acc.Amount3__c): 0);
        return null;
    }
}

I also tested the code in my DE environment and below is the output:

User-added image


Please let me know if it helps you.

Regards,
Mahesh
Preeti Khanna 10Preeti Khanna 10
Thanks for the reply!
Is it possible to achieve the same without command button and when the fields are updated.
Kindly suggest.
 
Mahesh DMahesh D
Hi Preeti,

Please find the modified code:
 
<!--  Page: -->
<apex:page standardController="Account" extensions="AccountSumExtController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount1__c}">
                    <apex:actionSupport event="onchange" action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount2__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount3__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId" />
                </apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:commandButton value="Calculate" action="{!calculate}"/> <br/><br/>
        
        <apex:outputLabel id="oId">Sum of Above 3 fields is: {!total}</apex:outputLabel>
        
    </apex:form>
</apex:page>

Controller is same and no change.

I also tested the above code and it looks good.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Preeti Khanna 10Preeti Khanna 10
Hi Mahesh,

Thanks alot for your help and it is working fine.

One more issue I have in same page I need to divide the total field by 55 and multiply by 100 .
As I am new to salesforce so not able to get the desired result.
 
Mahesh DMahesh D
Hi Preethi,

Please use the below code:
 
public class AccountSumExtController {
    public Account acc {get; set;}
    public Integer total {get; set;}
    public AccountSumExtController(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
        
        if(acc != null && acc.Id != null) {
            acc = [Select Id, Name, Amount1__c, Amount2__c, Amount3__c from Account where Id =: acc.Id];
        }
    }
    
    public PageReference calculate() {
        total = (acc.Amount1__c != null ? Integer.valueOf(acc.Amount1__c): 0) +
                (acc.Amount2__c != null ? Integer.valueOf(acc.Amount2__c): 0) +
                (acc.Amount3__c != null ? Integer.valueOf(acc.Amount3__c): 0);
        total = (total /55) * 100;
        return null;
    }
}
Regards,
Mahesh

 
Preeti Khanna 10Preeti Khanna 10
Hi Mahesh,

Thanks for your help!
I have tried the same but it is not working.
0 is displayed in total .
 I need to display it separately in percent field and it is displaying as 0 in that new field also.

Regards,
Preeti
Mahesh DMahesh D
Hi Preeti,

Please find the below code:
 
<!--  Page: -->
<apex:page standardController="Account" extensions="AccountSumExtController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount1__c}">
                    <apex:actionSupport event="onchange" action="{!calculate}" rerender="oId, oIdTwo" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount2__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId, oIdTwo" />
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:inputField value="{!acc.Amount3__c}">
                    <apex:actionSupport event="onchange"  action="{!calculate}" rerender="oId, oIdTwo" />
                </apex:inputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
        <apex:commandButton value="Calculate" action="{!calculate}"/> <br/><br/>
        
        <apex:outputLabel id="oId">Sum of Above 3 fields is: {!total}</apex:outputLabel> <br/>
        
        <apex:outputLabel id="oIdTwo">Percent of Above 3 fields is: {!percent}</apex:outputLabel>
        
    </apex:form>
</apex:page>
 
public class AccountSumExtController {
    public Account acc {get; set;}
    public Integer total {get; set;}
    public Decimal percent {get; set;}
    public AccountSumExtController(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
        
        if(acc != null && acc.Id != null) {
            acc = [Select Id, Name, Amount1__c, Amount2__c, Amount3__c from Account where Id =: acc.Id];
        }
    }
    
    public PageReference calculate() {
        total = (acc.Amount1__c != null ? Integer.valueOf(acc.Amount1__c): 0) +
                (acc.Amount2__c != null ? Integer.valueOf(acc.Amount2__c): 0) +
                (acc.Amount3__c != null ? Integer.valueOf(acc.Amount3__c): 0);
        percent = (total / 55)*100;
        return null;
    }
}

User-added image

Please do let me know if it helps you.

Regards,
Mahesh
Preeti Khanna 10Preeti Khanna 10
Hi Mahesh,

Thanks for the reply!
Issue I ma having is that my three fields are picklist fields and I am summing up the values in total which is displayed correctly.
But as it is picklist field so system is not calculating percent and displaying as zero.

Please help asap.

Regards,
Preeti Khanna
 
Navneet Shukla 14Navneet Shukla 14
Hi Mahesh,
i have tried this code but i cannot see any total or calculate field in aacount page as im new so please help!