+ Start a Discussion
raj161raj161 

Get the value from lookup field on Visulaforce

Hi,

I have kept a look up field in the visualforce page using the following code.

 

<apex:inputfield value="{!Account.Associated_Global_Account__c}"/>

 

The lookup field works good but when a user enter the information in the lookup field I am unable to capture it on the apex side.

 

I am not sure how to capture a value from the lookup field from the visualforce page.

 

 

 

 

 

aalbertaalbert

You need to have it posted back to the supporting controller. By simply populating that inputfield in the page won't set the apex controller properties. Do you have the user click a button, for example within an apex:form? Put a simple button on the page that calls an apex controller method. You should see the value then.

 

 

raj161raj161

Perfect!!!.

 

Thank U.

ssssssss

hi raj,

 

I am facing the problem how to capture the lookup filed value in apex controller.u got the solution plz give me example code.

 

Thanks in advance,

manu...

raj161raj161

Please make sure you include the standardcontroller code in apex page....I was missing this at first...:)

the code goes like this...

public GlobalAccountContacts(ApexPages.StandardController controller) { Id id = ApexPages.currentPage().getParameters().get('id'); this.act=(account)controller.getRecord(); }

 Let me know if any confusion....


 

 

ssssssss

Thank u so much for ur reply,

 

I have 2 VF pages with three common fileds.So i dont want to enter the common values again in second VF page.So for first filed i can catch the value through lookup.once i catch the value i want to populate the vaues of remaining two fileds of corresponding lookup value by clicking on command link.To populate the values i have to capture the lookup value in controller with out saving the record.By using getRecord() we can get the saved record value.

I am using standard controller only.

I should implement this concept in many pages.plz tell me how to capture the lookup filed value in controller without saving the record.

 

Thanks in advance,

Manu

 

 

 

anu_karthianu_karthi

Hi aalbert,

 

dats great to see that u could find a solution to pass a lookupfield value from the vpage to controller, actually iam having a similar issue...plz help me in  solving it..

my code is as follows....

 

<apex:smileytongue:age standardController="CAF_Bank__c" extensions="lookup3">
<script>
function branchpopulate()
  {
   <!-- here iam trying to fetch the list which is having queried output ie  branchname field value corresponding to record whose branchcode value==lookup field value values -->    
 var myArray = new Array();
  myArray= '{!CurrentBranchList}'.replace('[','').replace(']','').split(',');
            var currbranchname=myArray;
                  alert(currbranchname);

<!--but currbranchname is displaying some strange values but not the exact branch name corresponding to a record which i queried in d controller so plz correct me how to do this task of fetching that record related to my lookup field value and populate those values to ui ie visualforce page....-->                 document.getElementById('{!$Component.form1.branchname}').value=currbranchname;  

<!--here iam popuilating branch code value to the visualforce page inputfield value-->    
       return false;    
     }
 </script>
  <apex:form >
    <apex:smileytongue:ageBlock title="My Content">
        <apex:smileytongue:ageBlockSection title="My Content Section" columns="2">
          <apex:smileytongue:ageBlockSectionItem >
              <apex:smileysurprised:utputLabel value="BranchCode" for="theLookup"/>
               <apex:inputField id="theLookup" value="{!CAF_Bank__c.caf_lookup__c}" />

<!--i want to pass this lookup field valuue to my controller so i have to filter query using this value,but iam not able to pass it i am able to pass textfield value using setters and getters but iam not able to pass this lookup fielsd value plz tell me how to pass this value to controller -->

             </apex:smileytongue:ageBlockSectionItem>
               <apex:smileytongue:ageBlockSectionItem>
              <apex:smileysurprised:utputLabel value="Bank Name"/>
                <apex:inputField id="bname" value="{!CAF_Bank__c.Bank_Name__c}"/>
              </apex:smileytongue:ageBlockSectionItem>
              <apex:smileytongue:ageBlockSectionItem >
              <apex:smileysurprised:utputLabel value="Branch Name "/>
                <apex:inputField id="branchname" value="{!CAF_Bank__c.Branch_Name__c}"/>
              </apex:smileytongue:ageBlockSectionItem>
            </apex:smileytongue:ageBlockSection>
        </apex:smileytongue:ageBlock>
    <apex:commandButton value="autopopulating" onclick="branchpopulate()"/>
      </apex:form>
</apex:smileytongue:age>

 

 

My controller is as follows:

 

 

public class lookup3
{
List<String> CurrentBranchList=new List<String>();
public CAF_Bank__c caf {get; set;}
ApexPages.StandardController con;
public lookup3(ApexPages.StandardController controller) {
this.con = controller;
caf = [select caf_lookup__c from CAF_Bank__c limit 1];// trying to get the current lookup field to controller correct me if iam wrong//String lookedupvalue = caf.caf_lookup__c;
/*trying to fetch the value of the branchk name where bankname correspondind to current branchcode*/
CAF_Bank__c [] currentbranchnames=[SELECT  Branch_Name__c FROM CAF_Bank__c  where Branch_Code__c=:lookedupvalue];
for (CAF_Bank__c f :currentbranchnames)
{
CurrentBranchList.add(f.Branch_Name__c);//passing the queried output to the list so that i can pass this list to the vpage //}
}
public String[] getCurrentBranchList() {
return CurrentBranchList;
}
}

 

 So finally what iam trying to do here is just passing lookupfield value to controller and query the record related to that lookupfield value and pass thiss queried output to vpage and automatically populate those values to the input fieldsup onselecting any lookup field value in vfpage..

So bob if iam not clear to u abt  my requirement plz let me know.and plz correct my mistakes so that io can fulfil my requirement...

 

So hope iam going wrong in passing  lookup field value,since its not passed plz correct me in passing the lookupfield value to the  controllerr....

 

                                                                                                                                     Thanks&Regards,

                                                                                                                                             Anu....

 

 

 

ssssssss

Hi raj,

 

plz give me complete example for capturing the lookup value in apex side and to dispaly agian that lookup value in vf page by clicking on the button.So that i can understand.I am new to apex and vf.plz help me.

 

Thanks in advance,

Manu..

 

raj161raj161

VF Code....

In the below code  {!Account.Associated_Global_Account__c} is the lookupvalue.

<apex:page tabStyle="Global_Account_Team__tab" standardController="Account" extensions="GlobalAccountContacts" showHeader="true" sidebar="false">
<apex:form >
<apex:outputLabel style="font-weight:bold;" value="Account Name :"></apex:outputLabel>
<apex:inputField id="accountName1" value="{!Account.Associated_Global_Account__c}"/>
<apex:commandButton value="Search" action="{!Search}"/>

 The related controller code is here..

GlobalAccountContacts

 

public GlobalAccountContacts(ApexPages.StandardController controller) { Id id = ApexPages.currentPage().getParameters().get('id'); this.act=(account)controller.getRecord(); } String AccountId; AccountId = act.Associated_Global_Account__c;

 

 Raj

 

 

 

EvitaEvita

Hi,

 

      Can someone guide me through the steps of saving value from a textbox to database in Force.com. I have two textboxes - Product Name and ProductID and two buttons- Save & Get.

When Save button is clicked - value in Product Name textbox is saved in database and when a value is inserted in ProductID textbox and Get button is clicked corresponding ProductName is fetched from database in the Product Name textbox.

 

Basically I am looking for steps of simple data save and retrieval