How to auto populate lookup field values to other fields in Salesforce?

How to auto populate lookup field values to other fields in Salesforce?

Sample Code:


Visualforce page:

<apex:page sidebar=”false” controller=”SampleController” >
<apex:form >
    <apex:pageBlock id=”pg”>
        <apex:pageBlockSection columns=”1″>
            <apex:inputField value=”{!interest.Name}”/>
            <apex:inputField value=”{!interest.Member__c}”>
                <apex:actionSupport event=”onchange” action=”{!fetchMemberDetails}” reRender=”pg”/>
            </apex:inputField>
            <apex:inputField value=”{!interest.Member_Name__c}”/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value=”Save”/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

Apex Controller:


public class SampleController {
    public Interest__c interest {get;set;}
    public Member__c member {get;set;}
    public SampleController(){
        interest = new Interest__c();
    }
    
    public void fetchMemberDetails(){
        member = [SELECT Name FROM Member__c WHERE Id = : interest.Member__c];
        interest.Member_Name__c = member.Name;
    }
}


Output:

Cheers!!!

Leave a Reply