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
Rakesh Reddy1Rakesh Reddy1 

custom user login button on a vf page

Hello Friends,

I have a requirement where I need to have a 'login' button on my VF page and upon click it should function like how it is going to work when we logged in as a user from a user record. In my VF Page I created a picklist field to display all the user records and a login button. 

Can you Please tell me how can I write the logic to login button.

please find the code below i have written:

VF Page:
<apex:page controller="ListOfUsers">
  <apex:form >
       <apex:pageBlock >
         <apex:pageBlockSection >
                      <apex:OutputPanel title="Select User">
                                 <apex:selectList size="1" multiselect="false"  >
                                          <apex:selectOptions value="{!ListOfUsers}" />
                                 </apex:selectList>
                        </apex:OutputPanel>
          </apex:pageBlockSection>
          <apex:commandButton value="Login"  />
      </apex:pageBlock>
</apex:form>
</apex:page>

Controller:
public class ListOfUsers {
List<SelectOption> Options = new List<SelectOption>();
      public List<SelectOption> getListOfUsers()
    {
                  
              
               for(User u : [select id ,Username,name from user]  )
               {
                          Options.add(new SelectOption(u.Id , u.Name));
               }
              return Options;
    }
}
GarryPGarryP
here you go!
//APex controller
<pre>
public class ExampleController {

    public String selectedID { get; set; }
    public String ListOfUsers { get; set; }
    public ApexPages.StandardController controller;
    public ExampleController() {
    getListOfUsers();
    }
    public ExampleController (ApexPages.StandardController Controller) {
        this.controller = Controller;
    }
    public List<SelectOption> Options {get;set;}
    public void getListOfUsers(){
     Options = new List<SelectOption>();
        for (User u : [select id , Username, name from user]  ) {
            Options.add(new SelectOption(u.Id , u.Name));
        }
    }
    public Pagereference login(){
       Pagereference pageRed = new Pagereference('/servlet/servlet.su?oid=00D90000000ccBu&suorgadminid='+selectedID+'&retURL=%2F00590000000yBQF%3Fnoredirect%3D1%26isUserEntityOverride%3D1&targetURL=%2Fhome%2Fhome.jsp');
       pageRed.setRedirect(true);
       return pageRed;
    }
}
</pre>

//VF page
<pre>
<apex:page controller="ExampleController" showheader="false" sidebar="false">
  <apex:form >
       <apex:pageBlock >
         <apex:pageBlockSection >
                      <apex:OutputPanel title="Select User">
                                 <apex:selectList size="1" multiselect="false" id="userList" value="{!selectedID}">
                                          <apex:selectOptions value="{!Options}" />
                                 </apex:selectList>
                        </apex:OutputPanel>
          </apex:pageBlockSection>
          <apex:commandButton value="Login" action="{!login}" />
      </apex:pageBlock>
</apex:form>
</apex:page>
</pre>
GarryPGarryP
you will have to replace the oid=00D90000000ccBu with your org ID. put it in some custom setting or custom label