[SalesForce] calculation from input field using visualforce page

The TotalValue__c field being calculated on the fly/client side while the user is entering the field values and and so that they don’t have to do the calculation themselves.

TotalValue__c = ((Revenue1__c + Revenue2__c)*Term__c) + Fee1__c + Fee2__c .

Visualforce Page

<apex:page controller="examplecontroller" sidebar="false" tabStyle="Opportunity" >

<apex:form id="newform">

 <apex:sectionHeader Title="Example"  />

   <apex:outputPanel id="addRender" >
    <apex:repeat value="{!serlin}" var="s" id="theRepeat">            
    <apex:pageBlock id="block1">

     <apex:pageBlockSection collapsible="true" showHeader="true" title="Service" id="section1">               

                <apex:inputfield value="{!s.Revenue1__c}" required="true"/>
                <apex:inputField value="{!s.Revenue2__c}" required="true"/>
                <apex:inputField value="{!s.Fee1__c}"  required="true"/>
                <apex:inputField value="{!s.Fee2__c}" required="true"/>
                <apex:inputField value="{!s.Term__c}" required="true"  />
                <apex:inputField value="{!s.TotalValue__c}" required="true" />

    </apex:pageBlockSection> 


</apex:pageBlock>       
</apex:repeat> 
</apex:outputPanel>

</apex:form>

Controller

public with sharing class examplecontroller {


public LIst<example__c> serlin {get;set;}
public String OpportunityId{get;set;}



public examplecontroller() 
{
    try{

        serlin = new list<Service_Line__c>();
        OpportunityId =ApexPages.currentPage().getParameters().get('id');

        if(OpportunityId != null && OpportunityId != '') 
        {

            serlin =  [SELECT Id,Revenue1__c,Revenue2__c,Fee1__c,Fee2__c,Term__c,TotalValue__c FROM example__c WHERE Opportunity__c =:OpportunityId];
            System.debug('serlinlst:'+serlin);

        }
    } catch(Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
    } 

}

}

what is best way to achieve this one using jquery or javascript or visualforce components?

Thanks
Nagarjuna

Best Answer

This Update Total with Javascript has a JavaScript answer and a Salesforce re-render answer that if you are not comfortable with JavaScript and don't mind a small time delay is probably a better way to go.

Related Topic