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
SainSain 

Auto Populate Parent Object fields on the basis of selection of Lookup on child in VF Page

Hi,
I have requirment to auto populate parent object(Opporutnity) fields vales into child (Project__c) fields on the basis of selecting lookup field.
Please share the sample code.

VF Page:

<apex:page controller="autopopulateclass">
<apex:form >
<apex:pageblock >
<apex:pageBlockSection >
<apex:inputField value="{!pro.Name}"/>
</apex:pageBlockSection>
<apex:actionRegion >
 <apex:pageBlockSection id="accountInfo" title="Information of Account">
 <apex:inputField value="{!pro.Opportunity__c}">
 <apex:actionSupport event="onchange" action="{!populateRelAccFields}" rerender="accountInfo"/>
 </apex:inputField>
 <!--<apex:outputField value="{!opp.Account.Phone}"/>
 <apex:outputField value="{!opp.Account.Type}"/>-->
 <apex:inputField value="{!pro.Mobile_No__c}"/>
  <apex:inputField value="{!pro.Email__c}"/>
 </apex:pageBlockSection>
 </apex:actionRegion>
 <apex:commandButton value="Cancel" action="{!cancel}"/>
 <apex:commandButton value="Save" action="{!save}"/>
</apex:pageblock>
</apex:form>
</apex:page>

Controller:

public with sharing class autopopulateclass {
public project__c pro{get; set;}
public opportunity opp{get;set;}

public autopopulateclass(){
pro=new project__c();
opp=new opportunity();
}

public pagereference populateRelAccFields(){
if(null!=pro.opportunity__c){
project__c pro1=new project__c();
pro1.mobile_No__c=opp.mobile_No__c;
pro1.email__c=opp.email__c;

}
return null;
}
public void save(){}
public void cancel(){}
}

Thanks in advance
Sain