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
dmondmon 

Get VF page form field value from Apex

In theory (or by example), how can i get the Contact (or any other lookup) form field value before save or button clicks?

1. User performs lookup to change the Contact on a VF page.
2. Now, i want the Apex controller to get the new Contact value and run a query (prior to save button)
3. Re-render the address section with new info.
4. User saves (or clicks another button).

For clarity, i know how to query, render, save etc... i do not know how to "get the lookup obj form field value on a VF page to the Apex controller."

I am embarrassed that i have to ask for help on what seems very basic.
System.currentPageReference().getParameters().get('the_contact_id_form_field_id'); --does not return the value

No code to share, still trying to conceptulize how to grab that new lookup contact id and get it to the controller.

Thanks in advace,
Dale


Message Edited by dmon on 12-02-2008 10:40 AM

Message Edited by dmon on 12-02-2008 10:42 AM
JeremyKraybillJeremyKraybill
Hi Dale,

it is a little hard to be sure I'm helping without your code, but any inputField on your VF page that is bound to a merge field in your controller's model will have its value re-bound to the merge field when the form is submitted (whew!)

So if, for instance, the field that the user is using to "change the Contact on a VF page" is, for instance, defined like

<apex:inputField value="{!parentObject.Contact__c}" id="field1" />

Then however you are giving parentObject to the VF page, the Contact__c field on it will be set whenever you submit the form (via a commandButton, most likely).

Here is kind of a trivial example, but it shows how form fields get bound back to model objects.

controller:

public class ScratchpadController {

    private Contact myValue;
   
    public ScratchpadController() {
        myValue = new Contact();
        myValue.FirstName = 'Test Contact';
    }

    public Contact getContact() {
        return myValue;
    }
   
    public void doUpdate() {
        myValue.FirstName = myValue.FirstName + '...';
    }

}


VF page:
<apex:page controller="ScratchpadController">

<apex:form >
    <apex:commandButton action="{!doUpdate}" reRender="field1" value="Test" id="testButton" />
    <apex:inputField value="{!contact.FirstName}" id="field1" />
</apex:form>
</apex:page>


In this example, when you run the page, you see a text field with "Test Contact" as the content. When you edit that field, and then click the "Test" button, you will see whatever value you changed the field to, with "..." at the end.  During the server round trip, first the value of contact.FirstName gets bound back to the contact object (retrieved from the controller via a call to getContact() per VF convention), and then the action method gets called.

With a lookup it may be a little more complex, but should work with the same principle.

Based on your original question, I am fairly certain that what you are missing is that your inputField for the lookup should be bound to an SObject held by your controller (even if, as here, it's a totally temporary SObject that doesn't get persisted).

Hope that helps!