You need to sign in to do that
Don't have an account?
Passing Id of Lookup Field to String Onchange
Hello All,
I've been trying to come up with a way of doing the following and have so far been unsuccessful. Onchange of a lookup field I would like to assign the value of the Id to a string in the controller and perform an action method to rerender another section of the page based on that value. This is not what I am trying to do but is a very straight forward example of what I am trying to accomplish.
APEX Class
public string ContactId {get;set;}
public string getContactEmail(){
string ContactEmail = [Select Email from Contact where Id=: ContactId].Email;
return ContactEmail;
}
-----------
VF Page
<apex:inputfield value="{!Form__c.Contact__c}">
In this example I would like to assign the Id value of the contact from the Form__c.Contact__c inputfield to the ContactId string in the controller when the lookup field is changed. I have tried using javascript to acheive the lkid value of this field and several other ways with no success. Anyone have any ideas of assigning the Id value of a lookup field to a string on change without turning the lookup field into a select list (over 1000 records return so this is not an option). ?
You can do 3 things
1)
You must be haveing Form__c object instance in your controller , you can do like this
public string ContactId
{ get
{
if(fromObjInstance != null)
return fromObjInstance.Contact__c;
return null;
}
set;
}
2)You can use this just before query like this
public string ContactId {get;set;}
public string getContactEmail(){
ContactId = fromObjInstance.Contact__c;
string ContactEmail = [Select Email from Contact where Id=: ContactId].Email;
return ContactEmail;
}
3) You can use a hidden field
<apex:inputhidden value="{!contactid}" id="hdnContactId" />
assign value to hisen in java script according to selection in lookup before making server call so hidden value gets binded with contrller property.
All Answers
You can do 3 things
1)
You must be haveing Form__c object instance in your controller , you can do like this
public string ContactId
{ get
{
if(fromObjInstance != null)
return fromObjInstance.Contact__c;
return null;
}
set;
}
2)You can use this just before query like this
public string ContactId {get;set;}
public string getContactEmail(){
ContactId = fromObjInstance.Contact__c;
string ContactEmail = [Select Email from Contact where Id=: ContactId].Email;
return ContactEmail;
}
3) You can use a hidden field
<apex:inputhidden value="{!contactid}" id="hdnContactId" />
assign value to hisen in java script according to selection in lookup before making server call so hidden value gets binded with contrller property.
Thanks, that works well for editting an object record instance that is already inserted, do you have any idea how to retrieve the value for a record that is not already inserted?
To be a little more clear, the object instance is substantiated in the constructor through ApexPages.StandardController.getRecord(); If the record is new with no ID, there is no isntance of the record to retrieve the lookup field value from. How do I obtain this? See below for example
public class with sharing FormController{
public Form__c f;
public FormController(ApexPages.StandardController controller){
this.f = (Form__c).controller.getRecord();
}
public string ContactId {get{ return f.Contact__c;}set;}
}
<apex:page standardcontroller="Form__c" extensions="FormController">
<apex:form>
<apex:inputfield value="{!Form__c.Contact__c}"/>
</apex:form>
</apex:page>
The get ContactId retrieves the value for a record already substantiated but not if this page is used in creating a new record? Is there some other method to retrieve this value for a non substantiated instance of a record?
I figured it out, I added an apex:actionsupport to the field and assigned {!Form__c.Contact} to a string value I put in the controller - thanks again for your help!
can you provide me the code to read look up value in the controller for new object...if it is already solved
Thanks