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
lakslaks 

Autopopulate values on Lookup selection

Hi,

 

I have some textboxes in my VF page. And an Account lookup field.

 

I want to populate the textboxes with the value in Address field of the Account object which I am selecting via the Account lookup.

 

 

 

Regards,

Lakshmi.

Best Answer chosen by laks
lakslaks
Thank you for your suggestion. I couldn't try it out though. 

Revisiting the post after quite long. Just thought of posting what I had done to solve the same at that time. 

In the Visualforce page:
The lookup field:
<apex:inputField onchange="ChangeAccountInfo(this)" value="{!SL_Order__c.Opportunity__r.AccountId}"/>

JS function within <script></script> tag:
function ChangeAccountInfo(obj)
{      
ChangeAccInfo(); 
}

Action function:
<apex:actionFunction name="ChangeAccInfo" action="{!ChangeAccInformation}" reRender="ShippingInfo,PageBlButton,pagemessage">
</apex:actionFunction>

Controller method:
public void ChangeAccInformation()
{
//Logic to fetch the address information based on the Account ID selected
.........
........
}

All Answers

MiddhaMiddha

 

Follow these:

 

VF PAGE: On your VF page where you have added Account lookup, add actionSupport. On change of Account lookup value this will call another method in your controller which will query the Account record slected and populate other fields that you want. Also this will rerender other fields on the page with the new values you have just queried

 

<apex:inputField value="{!tObj.Account__c}">
        <apex:actionSupport event="onchange" rerender="panel1" action="{!populateOtherFields}"/> 
 </apex:inputField>

<apex:outputPanel layout="block" id="panel1">
	<apex:inputText value="{!accIndustry}"/>
	<apex:inputText value="{!accNumber}"/>
</apex:outputPanel>

 APEX CLASS:

public String accIndustry {get;set;}
public String accNumber {get;set;}
public PageReference populateOtherFields()
{
	Account a = [Select Id, Industry, AccountNumber from Account where Id=: tObj.Account__c];
	accIndustry = a.accIndustry;
	accNumber = a.AccountNumber;
	return null;
}

 

I have typped the above code for your reference, it might have some typos etc. Use this for your refernce and add error handling etc.

 

IMP: If you have some required fields on the page, you might face some issues while rerendering and have to use actionregion tag here. To validate this functionlity, which you select an Account, fill all the required fields on the page firsy, if you have any,

lakslaks
Thank you for your suggestion. I couldn't try it out though. 

Revisiting the post after quite long. Just thought of posting what I had done to solve the same at that time. 

In the Visualforce page:
The lookup field:
<apex:inputField onchange="ChangeAccountInfo(this)" value="{!SL_Order__c.Opportunity__r.AccountId}"/>

JS function within <script></script> tag:
function ChangeAccountInfo(obj)
{      
ChangeAccInfo(); 
}

Action function:
<apex:actionFunction name="ChangeAccInfo" action="{!ChangeAccInformation}" reRender="ShippingInfo,PageBlButton,pagemessage">
</apex:actionFunction>

Controller method:
public void ChangeAccInformation()
{
//Logic to fetch the address information based on the Account ID selected
.........
........
}
This was selected as the best answer