Play Games

Search This Blog

Thursday, June 5, 2014

How to do mathematical functions in visualforce page?


Generally we do mathematical operations in apex classes and get that values to visualforce page.

But there may be situations where we need to do mathematical operations in visualforce page itself.For example if we have professional edition,then we cannot create apex classes.
At that time we can do mathematical operations in vf page itself in the following way.

For example to find sum of list of values.first we create <apex:variabe> and
then inside <apex:repeat> tag we perform the mathematical calculations.

The below example will find the sum of a field(quantity) of all opportunitylineitems
associated with opportunity.

<apex:page standardController="opportunity" >
<apex:form >
     
          <apex:repeat value="{!opportunity}" var="qList">
          <apex:variable value="{!0}" var="salesprice"/>
          <apex:variable value="{!0}" var="sumofquantity"/>
             <apex:repeat value="{!qList.opportunitylineitems}" var="QlItem">
                 <apex:variable value="{!salesprice +  if(QlItem.Unitprice!= null,QlItem.Unitprice,0)}" var="salesprice"/>
                 <apex:variable value="{!sumofquantity +  if(QlItem.Quantity!= null,QlItem.Quantity,0)}" var="sumofquantity"/>
                 <apex:outputLabel >Total Quantity</apex:outputLabel><apex:outputLabel value="{!sumofquantity}" title="Total"/>  <br />  
                 <apex:outputLabel >Total Sales Price</apex:outputLabel><apex:outputLabel value="{!salesprice}" title="Total1"/>  <br />  
                 <apex:outputLabel value="MC= {!if(QlItem.Unitprice == 0,0,round(QlItem.Unitprice/QlItem.Quantity,2))}"/>
             </apex:repeat>

    </apex:repeat>
    
</apex:form>
</apex:page>

 When we run the page by giving opportunity id,the output  will be as follows















                                                                                                                                                                                                                        

No comments:

Post a Comment