0

I have two input field of type number Field1 and field2 on Opportunity page . Here is my requirement If i enter field1= 5 then field2 should auto populate 30+5=35 on the same screen.

I am struggling in it since 2 days.

Please help me out. Thanks in advance!

  • Please explain a bit more. Can user edit the field2? What about the value 30? Is it a constant? – Saroj Bera Jul 13 '15 at 14:13
  • Yes 30 is a constant , No user cant edit field2 value. But editing the field2 is not my concern i want sum 30+ field1 on onclick function. – Cloud Arch Jul 13 '15 at 14:15
  • When should the onclick function get fired? Is it a save button? – Saroj Bera Jul 13 '15 at 14:19
  • when user enter the value on field1 – Cloud Arch Jul 13 '15 at 14:19
  • As I commented on your other question: How are you struggeling with this ? What isn't working in your code ? What is blocking you from solving this yourself ? Please try to explain us what you need to know exactly and share your work so far. – Samuel De Rycke Jul 13 '15 at 15:24
1

Add an onchange attribute to the apex:inputField (Field1) that calls a JavaScript function when Field1 is changed to evaluate the value you want for Field2, e.g.:

function updateField2(field1val) { 
  document.getElementById(yourField2ID).value = field1val.value + 30;
}

<apex:inputField onchange="updateField2(this);" id="field1">

Take care in accessing VF elements from JavaScript, as it can be a little frustrating based on how Visualforce generates the HTML - you can either use $Component or use the generated element tree syntax.

| improve this answer | |
  • You provided more clear requirements after I posted this - if you don't want users to be able to edit Field2 at all, then I recommend @SarojBera's answer – Brian Mansfield Jul 13 '15 at 15:15
  • I have used your Code as below <srcipt> var Fieldval = document.getElementById('{!$Component.Field1}').value function updateField2(Fieldval) { document.getElementById('{!$Component.Field2}').value = document.getElementById('{!$Component.Field1}').value + 30; } </script> <apex:inputField value="{!Opportunity.No_of_Terminal__c}" id="Field2" /> <apex:inputField value="{!Opportunity.No_of_Additional_Terminal__c}" onchange="updateField2(this);" id="Field1" /> But it is giving the output 2430 if i am entering 24 in field1 – Cloud Arch Jul 13 '15 at 19:01
  • Change the first line of the function to document.getElementById(yourField2ID).value = Number(field1val.value) + 30;. However, if I am understanding your new requirements correctly, I still recommend using a formula field as Saroj suggested. – Brian Mansfield Jul 13 '15 at 19:46
  • i want field2 value to be rendered before save. Can i show formula field on New page of any object? (I guess no) – Cloud Arch Jul 15 '15 at 6:45
0

Create one number field (field 1) then create one formula field (type: Number). Put in the formula as Test_Number__c + 30, where Test_Number__c is the first number field. This should suffice your need as stated. Please note that the formula field will not be shown in the edit page but the value will be displayed in detail page after you save the Opportunity record.

| improve this answer | |
0

If the formula is just for display purposes you could use apex:variable with rerender to display your total, here's a quick example:

<apex:page standardController="Account">
    <apex:form>
        <apex:inputField value="{!Account.NumberofLocations__c}">
            <apex:actionSupport event="onchange" reRender="calc"/>
        </apex:inputField>
        <apex:variable value="{!Account.NumberofLocations__c + 30}" var="calc"/>
        <apex:outputText id="calc" value="{!calc}"/>
    </apex:form>
</apex:page>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.