I have created a Vf page from which i need to fetch the value of new owner and need to save the value in property defined in controller:

Following is my vf page code:

    <apex:InputField label="user" value="{!a.ownerid}"/>

here a is object of opportunity

Controller code:

used one property to store the value:

public string ownername {get;set;}

Note: user will input the value through lookup field.

Any suggestions on this?

2 Answers 2


When you submit the page, Visualforce will update the OwnerId field of your opportunity object a automatically for you. If you want to use that value elsewhere (e.g. in a save method), you can just reference the value directly:

Id newApproverId = a.OwnerId;

and you can query for the corresponding name (if you need that):

String newApproverName = [select Name from User where Id = :a.OwnerId].Name;

The inputfield will have a direct binding to the a.ownerid.

This binding is two-directional, so that means two things:

  1. When the page loads, the input field will be filled with a.ownerid.
  2. The reverse is also true: the value of the input field will be saved to a.ownerid. If you had a commandButton with an action that is executed on the controller, the a.ownerid will by then have the value that the user entered on the Visualforce page before the user clicked the commandButton.

Update your Visualforce code like so to include that commandButton

    <apex:InputField label="user" value="{!a.ownerid}"/>
    <apex:commandButton action="{!someAction}" value="Save"/>

Now you can reference a.owernid anywhere in that someAction method on the controller, and use code like this to store it to owername:

public PageReferene someAction() {
    ownername = a.ownerid;

You must log in to answer this question.

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