[SalesForce] How to get a value from a look up input field

I am using the apex:actionSupport control to execute some code when the apex:inputField it is associated to, has its value changed. The apex:inputField is a look up field, so it spawns a new window to do the look up and then dutifully returns the value to the control on the page. The onchange event of the apex:actionSupport control then fires, but in my apex handler, the object the apex:inputField is bound to, does not have the value populated. Any ideas how I can get this value?

Here's my code:

<apex:inputField id="ShareInputField" value="{!Share.Value__c}"> 
  <apex:actionSupport onsubmit="alert(this.value);" event="onchange" action="{!GetTarget}" immediate="true" reRender="ShareForm"/>
</apex:inputField> 

public void GetTarget(){ system.debug('USR_MSG: ' + this.Share.Value__c); } 

Best Answer

The main issue is that immediate=true flag. When that's set, the action method is fired before the getters and setters. This is useful for things like cancelling, when you don't actually need to process anything. However, it's preventing the controller from getting your values.

As you mentioned, removing this causes validation failures else where. You'll want to use the apex:actionregion tag to resolve this. With this in place, only that portion of the form is processed, so validations present elsewhere won't get run.

<apex:actionRegion>
  <apex:inputField id="ShareInputField" value="{!Share.Value__c}"> 
    <apex:actionSupport onsubmit="alert(this.value);" event="onchange" action="{!GetTarget}" reRender="ShareForm"/>
  </apex:inputField>
</apex:actionRegion>