0

I have a requirement is when I select the name from the the drop down automatically the email id will appear below the name.

//Here I am getting all the user
 public List<SelectOption> UserList
{
    get
    {
        UserTemp = [Select u.LastName, u.Id, u.FirstName From User u ];            
        UserList = new List<SelectOption>();           
        for(User temp : UserTemp)
        {
            UserList.add(new SelectOption(temp.Id, temp.FirstName+' '+temp.LastName));               
        }
        return UserList;
    }
    set;
}

// Below is my VF code for user drop down

<apex:selectList size="1">
 Full Name: 
<apex:selectOptions value="{!UserList}"></apex:selectOptions>
</apex:selectList>
<apex:selectList size="1">

Here what I am trying to get the email and need to set to the VF page

public User user { get; set; }
public String UserId { get; set; }

public PageReference loadDetails(){
     user = [Select u.email From User u Where u.Id = :UserId ];
return user;
}

But not getting idea how to pass the UserId from VF page to get the Email Id.

1

You will have to capture the selected value in UserId variable and then use actionSupport to rerender the component you wish to render again.

Your selectList should be

<apex:selectList value="{!UserId}" size="1">
 Full Name: 
    <apex:selectOptions value="{!UserList}"></apex:selectOptions>
    <apex:actionSupport event="onchange" reRender="userEmail" action="{!loadDetails}"/>
</apex:selectList>
<apex:outputText id="userEmail" value={!user.Email}/>
0

Visual force Page : when we select any user , we store that user id into the
selectedUser , and then same will do query on users for getting the user email, and just reRender pageblock when we select anyone from the list.

<apex:page controller="userListController">
        <apex:pageBlock id="pageblk">
        <apex:form>
        <apex:pageBlockSection>
            <apex:pageBlockSectionItem> 
                <apex:selectList size="1" value="{!selectedUser}">  
                    <apex:actionSupport event="onchange" action="{!loademail}" reRender="pageblk"/>
                    <apex:selectOptions value="{!UserList}"/>
                <!--apex:selectOptions value="{!UserList}" onchange=""></apex:selectOptions-->
                </apex:selectList>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem>
                <apex:outputLabel>{!userDetails.email}</apex:outputLabel>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:form>
        </apex:pageBlock>
    </apex:page>

Apex controller:

 public class userListController {
    public User userDetails { get; set; }
    public String UserId { get; set; }
    public List<SelectOption> UserList {get;set;}
     public Id selectedUser {get; set;}

       public userListController(){
            List<User> usr = new List<User>();
            userDetails = new User();
                usr = [Select LastName,Id,FirstName From User ];            
                UserList = new List<SelectOption>();           
                    for(User temp : usr)
                    {
                        UserList.add(new SelectOption(temp.Id, temp.FirstName+' '+temp.LastName));               
                    }
        }
        public void loademail(){
            System.debug('---selectedUser'+selectedUser);

         userDetails = [Select id,email From User Where Id = :selectedUser ];

        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.