I have VF page where there is lookup field e.g. XYZ. After selcting a record from XYZ, I want to automatically render ABC record related to XYZ, where ABC is an Inputfield. I have written method called onchange of that XYZ lookup field.

Now I want to access the value in the XYZ input field to use that in my SOQL query to get its related record i.e. ABC record. BUT I am not getting the record values in that method. Not even in constructor (obvious). I have taken StandardController of the object too.

  • It's hard to give you an answer with the limited data you gave us. Did you bind the data to your controller? This should be the simplest way.
    – dphil
    Commented Jun 11, 2014 at 13:47
  • The onchange event Property where will you use either Script/Controller Commented Jun 11, 2014 at 14:08
  • Nilesh, you'll have to share your code with us, it's very hard to understand without it. Are you using remote javascript or plain rerendering/action methods ? Commented Jun 11, 2014 at 14:11
  • No JavaScript, its just one VF page and its and extension controller. Lets make it simple - Can I fetch the value of my Inputfield in the controller of the same page ? Commented Jun 12, 2014 at 5:31
  • @All ---- It was required field (field ABC) and I just had to make it outputField instead of inputfield. Thanks Commented Jun 12, 2014 at 5:47

1 Answer 1


Hi you can achieve by using Action Function Easily. Below is the VF page using standard Task controller with extension and I am taking Account lookup value and passing it to extension to use in SOQL for selecting related contacts, and populating related contacts in VF page. Hope this will help you.

<apex:page standardController="Task" extensions="Test1Ext" id="thepage">


function fetchContactDetails(acc){


<apex:form id="frm" >
<apex:actionFunction name="fetchContact" action="{!createavailableContactList}" rerender="contacts" immediate="true">
<apex:param name="accountid" value=""/>

<apex:pageBlock id="pb" >

<apex:pageBlockButtons id="pbb" >

    <apex:commandButton action="{!Save}" value="Save" />
    <apex:commandButton action="{!Cancel}" value="Cancel"/>       


   <apex:pageblockSection id="pbsection">

        <apex:inputField value="{!Task.WhatId}" onchange="fetchContactDetails()" onselect="fetchContactDetails()" id="whatidfield"/>
        <apex:pageblockSectionItem >
        <apex:outputLabel value="Contact"  />
        <apex:selectList value="{!contactselected}"  id="contacts" size="1">
        <apex:selectOptions value="{!ContactList}"/>
        </apex:selectList> </apex:pageblockSectionItem>                           
        <apex:inputField value="{!Task.ActivityDate}" />
        <apex:inputField value="{!Task.Description}" />                     



Extension code:

public with sharing class Test1Ext {

public string contactselected{get;set;}    
public List<Contact> availableContactList { get; set;}
public List<SelectOption>  ContactList {get;set;}

public Test1Ext(ApexPages.StandardController controller) {
ContactList = new List<SelectOption>();
ContactList.add(new SelectOption('','--None--'));


 public void createavailableContactList(){
    availableContactList = [Select id, Name from Contact where Accountid =:Apexpages.currentpage().getparameters().get('accountid') ];

     for(integer i=0;i<availableContactList.size();i++)   {
       ContactList .add(new SelectOption(availableContactList[i].id,availableContactList[i].Name));



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .