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
Eager-2-LearnEager-2-Learn 

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>

 

aballardaballard

Since you are binding the InputField to NewContactOwner.OwnerId, the selected new owner should be in that object when the changeOwner method is called. 

Eager-2-LearnEager-2-Learn

How do I access it through apex?

myforcedotcommyforcedotcom

Tom in your method changeOwner() you would do the following:

 

PageReference changeOwner(){

     String uId = NewContactOwner.OwnerId;

     //do stuff with the uId

}

 

 

Eager-2-LearnEager-2-Learn

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?

 

<apex:page controller="CFCT_ContactOwnerChange_Controller" Title="Contact Owner Mass Change" tabStyle="Contact">
    <apex:pageMessages escape="false" />
    <apex:form >
        <apex:pageBlock title="Contact Owner Mass Change">
            <apex:pageBlockSection columns="1">  
            <apex:pageblockSectionItem >
                <apex:outputLabel value="Current Owner" for="CurrentContactOwner" />
                <apex:outputPanel layout="block" styleClass="requiredInput">
                    <apex:outputPanel layout="block" styleClass="requiredBlock"/>                
                    <apex:selectList value="{!CurrentContactOwner}" id="CurrentContactOwner" size="1">
                        <apex:selectOptions value="{!currentContactOwners}"/>             
                    </apex:selectList>                   
                </apex:outputPanel>
            </apex:pageblockSectionItem>              
            <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>
        <apex:outputLabel ><font color="Red"><b>Once the "Change Owner" button is clicked this action cannot be un-done.  Choose the Current and New Owner Wisely</b></font></apex:outputLabel>
    </apex:form>    
</apex:page>

 

aballardaballard

I think we need to see your controller code too. 

Eager-2-LearnEager-2-Learn

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.

public class CFCT_ContactOwnerChange_Controller {

    private String currentContactOwnerId;
    private String ownerId;
    private String ownerName;
    private String newOwner;
    
    /* Getter which dynamically generates a unique list  
       of contact owners from the Contact object */        
    public List<SelectOption> getcurrentContactOwners() {      
        List<SelectOption> optionList = new List<SelectOption>(); 
        /* Add a null option to force the user to make a selection. */
        optionList.add(new SelectOption('','--None--'));  
        /* Loop through the Contact records creating a selectOption  
        for each result with the owner Id as the value and the owner name 
        as the label displayed in the selectList */  
        for (AggregateResult c : [ SELECT ownerid, owner.name ownername 
                                   FROM contact 
                                   GROUP BY ownerid, owner.name Limit 500 ]) {
            ownerId = (String) c.get('ownerid');
            ownerName = (String) c.get('ownername');             
            optionList.add(new SelectOption(ownerId, ownerName));  
        }  
        return optionList;
    }   
         
    public void setCurrentContactOwner(String cco) { 
        this.currentContactOwnerId = cco; 
    }    
    
    public String getCurrentContactOwner() {
        return this.currentContactOwnerId;
    }

    public PageReference changeOwner() {
        //PageReference pageRef = ApexPages.currentPage();        
        //************************************************
        // ADD CODE TO UPDATE THE CURRENT OWNER TO THE NEW
        // OWNER FOR ALL CONTACTS
        //************************************************        
         
        System.debug('zz1: ' + this.currentContactOwnerId);
        if ( this.currentContactOwnerId == null) {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please choose a Current Owner');  
          ApexPages.addMessage(myMsg);
        }
        return null;
    }

                                        
   
   public Contact getNewContactOwner() {  
     return null;                                          
   }

}

 

myforcedotcommyforcedotcom

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.

 

public class CFCT_ContactOwnerChange_Controller {

    private String currentContactOwnerId;
    private String ownerId;
    private String ownerName;
    private String newOwner;
    private Contact myContact;
	
	//when the controller is called get the contact based on the id param
	public CFCT_ContactOwnerChange_Controller() {
            myContact = [select id, name, OwnerId from Contact where id =
                       :ApexPages.currentPage().getParameters().get('id')];
      }
	
    /* Getter which dynamically generates a unique list  
       of contact owners from the Contact object */        
    public List<SelectOption> getcurrentContactOwners() {      
        List<SelectOption> optionList = new List<SelectOption>(); 
        /* Add a null option to force the user to make a selection. */
        optionList.add(new SelectOption('','--None--'));  
        /* Loop through the Contact records creating a selectOption  
        for each result with the owner Id as the value and the owner name 
        as the label displayed in the selectList */  
        for (AggregateResult c : [ SELECT ownerid, owner.name ownername 
                                   FROM contact 
                                   GROUP BY ownerid, owner.name Limit 500 ]) {
            ownerId = (String) c.get('ownerid');
            ownerName = (String) c.get('ownername');             
            optionList.add(new SelectOption(ownerId, ownerName));  
        }  
        return optionList;
    }   
         
    public void setCurrentContactOwner(String cco) { 
        this.currentContactOwnerId = cco; 
    }    
    
    public String getCurrentContactOwner() {
        return this.myContact.Id;
    }

    public PageReference changeOwner() {
        //PageReference pageRef = ApexPages.currentPage();        
        //************************************************
        // ADD CODE TO UPDATE THE CURRENT OWNER TO THE NEW
        // OWNER FOR ALL CONTACTS
        //************************************************        
         
		PageReference pageRef;
        
		if (this.myContact.Id == null) {
		  //put error on page and send them back to choose a new owner.
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please choose a Current Owner');  
          ApexPages.addMessage(myMsg);
		  pageRef = null;
        }
		else{
		  //Do Something with new Owner Id
		  //send them back to the contact they came from
		  pageRef = new PageReference('/' + myContact.Id);      
		}
        return pageRef;
    }
   
   public Contact getNewContactOwner() {  
     return myContact;                                          
   }

}

 

I added a constructor to get the Contact sObject the user came from and changed the changeOwner and getNewContactOwner methods.

 

hope this helps

 

Eager-2-LearnEager-2-Learn

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! :(