+ Start a Discussion
Chandra babu 2Chandra babu 2 

how two sum two custom fields and Display the values in another column.

User-added image

i will give Amount and Discount Values,But i want  Two values Calculated display the netprice in UI page.plese tel me.
 
Alexander TsitsuraAlexander Tsitsura
Hi,

You need to create apex controller, for summarize this fields.

For example vf page
<apex:page controller="AmountController">
  <apex:form id="frm">
      <apex:pageBlock >
          <apex:pageBlockSection columns="1">
              <apex:inputText label="Amount" value="{!amount}" >
                  <apex:actionSupport event="onchange" reRender="frm"/>
              </apex:inputText>
              
              <apex:inputText label="Discount" value="{!discount}" >
                  <apex:actionSupport event="onchange" reRender="frm"/>
              </apex:inputText>
              
              <apex:outputText value="{0, number, currency}" label="net Price" >
                  <apex:param value="{!newPrice}" />
              </apex:outputText>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>
and apex controller
public with sharing class AmountController {
    public Decimal amount {
        get;
        set {
            // when changed amount value, recalc newPrice value
            if (value != amount) {
                amount = value;
                recalcNewPrice();
            }
        }
    }
    
    public Decimal discount {
        get;
        set {
            // when changed discount value, recalc newPrice value
            if (value != discount) {
                discount = value;
                recalcNewPrice();
            }
        }
    }
    
    public Decimal newPrice {
        get;
        private set;
    }
    
    public AmountController() {
        this.amount = 0;
        this.discount = 0;
    }
    
    private void recalcNewPrice() {
        newPrice = (null == amount ? 0 : amount) 
                 + (null == discount ? 0 : discount);
    }
}

 
Ajay K DubediAjay K Dubedi
Hi Chandra,

The below code can be used as per ur requirement. But in the following code i am using an controller for this and not using any fields. If you want to use any custom fields on any object than you can query the field value in place of <apex:inputtext> and modify the code as per your requirment.
<----------Controller----------->
public with sharing class sumcls {
    public integer sum {get;set;}
    public integer discount {get;set;}
    public integer out {get;set;}
    
    public sumcls(){
        
    }
    
    public pagereference result(){
        out = sum-discount;
        return null;
    }
}
<---------Page---------->
<apex:page controller="sumcls">
    <apex:form>
    <apex:actionFunction name="test" action="{!result}" reRender="go">                     
    </apex:actionFunction>   
        <apex:pageblock>
            <apex:inputText value="{!sum}" label="Sum"/> <br> </br>
            <apex:inputText value="{!discount}" label="Discount" onkeyup="test();"/>
      <!--      <apex:pageBlockButtons>
                <apex:commandButton action="{!result}" value="result"/>   
            </apex:pageBlockButtons>   -->
            <apex:outputText value="{!out}" label="this is result" id="go"/>
        </apex:pageblock>
    </apex:form>
</apex:page>
Thanks.
sslodhi87sslodhi87
You can use the javascript to calculate the value and assign to output field with out any server call