function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hemanth Tanguturi 14hemanth Tanguturi 14 

auto populate the field based on the other field from another object records

Hi,

I have created a visual force page 2 fields class and fee. when ever the class is selected or changed from picklist--the fee field should be refreshed based on the amount value we soql from fees__c object.
used action support with params with on change event of class picklist
Problem: the render is happening on first change of picklist only???
the fee field is not displayed on changing the picklist field???

Here is the code:

VF Page:
<apex:page standardController="Student_Reg__c" extensions="stuExt1">
<apex:form >
<apex:pagemessages ></apex:pagemessages>
<apex:pageblock >
<apex:pageblocksection title="Student Details" >
<apex:inputText value="{!Student_Reg__c.First_Name__c}"/>
<apex:inputField value="{!Student_Reg__c.Last_Name__c}"/>
<apex:inputField value="{!Student_Reg__c.Date_Of_Birth__c}"/>
<apex:inputField value="{!Student_Reg__c.Class__c}">
<apex:actionsupport rerender="fee_info" event="onchange" action="{!changeFee}">
<apex:param name="cid" value="{!Student_Reg__c.Class__c}"/>
</apex:actionsupport>
</apex:inputField>
</apex:pageblocksection>
<apex:pageBlockSection title="Fee Details" id="fee_info">
<apex:inputField value="{!fe.amount__c}"/>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>


Extension:
public class stuExt1 {

    string classs=ApexPages.currentPage().getParameters().get('cid');
    public fees__c fe {get;set;}
    public stuExt1(ApexPages.StandardController stdController) {

    }
    
    public void changeFee(){
    integer f=integer.valueof(classs);
    system.debug(f);
    fees__c fe=[select branch__c,class__c,Amount__c from fees__c where class__c=:f limit 1];
        
    }

}
Vigneshwaran GurunathanVigneshwaran Gurunathan
VF Page:
    <apex:page standardController="Student_Reg__c" extensions="stuExt1">
        <apex:form >
            <apex:pagemessages ></apex:pagemessages>
            <apex:pageblock >
                <apex:pageblocksection title="Student Details" >
                    <apex:inputText value="{!Student_Reg__c.First_Name__c}"/>
                    <apex:inputField value="{!Student_Reg__c.Last_Name__c}"/>
                    <apex:inputField value="{!Student_Reg__c.Date_Of_Birth__c}"/>
                    <apex:inputField value="{!Student_Reg__c.Class__c}">
                        <apex:actionsupport rerender="fee_info" event="onchange" action="{!changeFee}">
                            <!--<apex:param name="cid" value="{!Student_Reg__c.Class__c}"/>-->
                        </apex:actionsupport>
                    </apex:inputField>
                </apex:pageblocksection>
                <apex:pageBlockSection title="Fee Details" id="fee_info">
                    <apex:inputField value="{!fe.amount__c}"/>
                </apex:pageBlockSection>
            </apex:pageblock>
        </apex:form>
    </apex:page>


Extension:
public class stuExt1 {

    string classs=ApexPages.currentPage().getParameters().get('cid');
    public fees__c fe {get;set;}
    public Student_Reg__c student {get;set;}
    public stuExt1(ApexPages.StandardController stdController) {
        student = (Student_Reg__c)stdController.getRecord();
    }
    
    public void changeFee(){
        classs = student.Class__c;
        integer f=integer.valueof(classs);
        system.debug(f);
        fees__c fe=[select branch__c,class__c,Amount__c from fees__c where class__c=:f limit 1];       
    }
}

Explanation:

1. No need of apex:param as it didn't assign anything to any variable.
2. Added student variable to hold the values and assign the current values to it by stdController.getRecord()
3. Get student.Class__c in changeFee() method to get the updated selected value and query using that value.

Hope it helps.