+ Start a Discussion
rtandon17@gmail.comrtandon17@gmail.com 

Real time or dynamic calculation of field value based in visual force page

 

Hi,

I have a requirement where in I have table with 6 fields. The first 5 fields are picklist and sixth one needs to be calculated and should contain the result.

Now ,the user can select any values from each of the 5 picklists and based on the selection of values from the picklist , I will need to perform some numeric calculation and display the numerica value in the 6th field on the fly.

 

Currently my sixth field is a formula field , but the issue is , that formula field calculates and displays only after clicking on the save; Whereas my requirement is that the user  can see the change in the value of the 6th field on the fly as and when the user selects or changes the value in any of the first 5 picklist fields.

 

 

Please help!

Any complete sample code or javascript is appreciated. Thank you!

GlynAGlynA

I can offer you some code fragments that will hopefully serve as an example of one way to do what you are trying to do.

 

In your VF page, give each of your picklist fields an actionSupport component that triggers "onchange" and rerenders the result field.  the result field should not be the actual formula field from the record, but a text or numeric property in your controller.  (I don't know the name of your object or fields, so I've made up some names in the code below.)

 

    <apex:outputField value="{!My_Object__c.Picklist_1__c}">
        <apex:actionSupport event="onchange" rerender="theResult"/>
    </apex:outputField>
    <apex:outputField value="{!My_Object__c.Picklist_2__c}">
        <apex:actionSupport event="onchange" rerender="theResult"/>
    </apex:outputField>
    <apex:outputText value="{!theResult}" id="theResult"/>

 

In your controller, create a property that returns the result of the computation based on the values of the other fields.  In my example, I've created a property that returns a string, but it could return any type, and the computation could be as complex as necessary.

 

public String theResult
{
    get
    {
        return theRecord.Picklist_1__c + theRecord.Picklist_2__c;
    }
}

Here I assume that the controller has a member, 'theRecord', that is the record being displayed in the page.

 

Whenever the user changes the values of one of the source fields (which are picklists in your application, but they could be any type), the actionSupport will fire, causing the component with the id, "theResult", to be rerendered.  This in turn gets the new value of the property, "theResult".

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator