[SalesForce] how to pass value from visual force page (InputField) to controller

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:pageBlockSection>
    <apex:InputField label="user" value="{!a.ownerid}"/>
</apex:pageBlockSection>

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?

Best Answer

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;
...
Related Topic