You need to sign in to do that
Don't have an account?
How to reference an inputField in Apex after selecting a user
Hi,
I had a nother post but I think I may have had to much dialog and lost causing it to lose the intent.
The following vf page snippet allows me to click on the magnify glass and select an active user from a dialog box list. Once the user is selected they show up in the field. Now when I click the button I want to be able to access that value (user id or name) within apex so that I can do what I need to do. I am not sure how to get to the value in the field? Preferable I would want to get to the user id in the code but what shows on the vf page is the user name which is what the user needs to see.
<apex:pageBlockSectionItem > <apex:outputLabel value="New Owner" for="theNewContactOwnerLookup"/> <apex:inputField id="theNewContactOwnerLookup" value="{!NewContactOwner.OwnerId}"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageblockButtons > <apex:commandButton value="Change Owner" action="{!changeOwner}"/> </apex:pageblockButtons> </apex:pageBlock>
Since you are binding the InputField to NewContactOwner.OwnerId, the selected new owner should be in that object when the changeOwner method is called.
How do I access it through apex?
Tom in your method changeOwner() you would do the following:
PageReference changeOwner(){
String uId = NewContactOwner.OwnerId;
//do stuff with the uId
}
That does not work! I get the following error
Error: Compile Error: Variable does not exist: NewContactOwner.OwnerId at line 47 column 22
Below is the complete vf page. Does that help you help me?
I think we need to see your controller code too.
This the controller code. It seem like there needs to be a way to detect when the user dialog box closes and the value resides in the inputField it has to pass that to my controller so that I can use it.
Tom,
To get the value back into the controller after the lookup field is filled, you need to click a command button. I modified your controller code below. This should work for you.
I added a constructor to get the Contact sObject the user came from and changed the changeOwner and getNewContactOwner methods.
hope this helps
Thanks for the code-feedback. I am getting the following error when loading the vf page:
System.QueryException: List has no rows for assignment to SObject
Class.CFCT_ContactOwnerChange_Controller.: line 11, column 25 External entry point
I had something similiar to your suggested line of code below but it had a if else condition that returned null if no id was available and that prevented the error that I am getting now.
myContact = [select id, name, OwnerId from Contact where id = :ApexPages.currentPage().getParameters().get('id')];
My page is loading with no reference to contacts because I just want a page to load with two fields (Current Owner and New Owner). The user will select the current owner and a new owner. My code will search all contacts with the current owner name and change them to the new owner name.
My issues is the built in look up functionaliy that SFDC provides by referencing contact.ownerid does not seem to allow me to get access to the value once it is populated into the field!!
if you load the original visual force page and controller that I posted and simply go the page then select a new user you will see that my apex does not have a way to get the value that is put in the new owner field. That is what I need an answer for! :(
Thank you so much for assisting with this what seems to be a simple solution that has turned into hours/days of no success! :(