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
Prasanthi BPrasanthi B 

Passing selected records using an input checkbox field to a controller class

Hi,

 

I need to pass some records selected using an input checkbox field on a visualforce page to a controller. The list of selected records have to be updated with already selected lookup field value on the same visual force page.

 

Hence, the list of selected records and lookup field value should be passes over to a controller class.

 

Please find below code and suggest if there is an idea.

 

VFCODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
    
   <apex:inputField id="startMode" value="{!User.Region__c}" />
   <br></br>
      <apex:commandButton action="{!doSearch}" value="Search" >               
   </apex:commandButton>
  <br></br> 
  <apex:pageBlockSection >
   <apex:inputField value="{!User.ManagerId}" id="Check"/>
  </apex:pageBlockSection>       
  <apex:pageBlockTable value="{!results}" var="t">
        <apex:column headerValue="Select">
        <apex:inputCheckbox id="checkedone">
        </apex:inputCheckbox></apex:column>
        <apex:column value="{!t.Name}"/>
        <apex:column value="{!t.Email}"/>
        <apex:column value="{!t.Region__c}"/>
        <apex:column value="{!t.Manager__c}"/>
      
  </apex:pageBlockTable> 
  <apex:commandButton value="UpdateManager" action="{!UpdateManager}"/>
  </apex:pageblock>    
  </apex:form>
  </apex:page>

 

CONTROLLER:

 

public class UserData {
PUBLIC String usr{get;set;}
PUBLIC List<selectOption> usrs;
List<User> results=new List<User>();

public User user;
String Regionid;
String Region;
  public UserData(ApexPages.StandardController controller)
   {
     this.user= (User)controller.getRecord();
   }
    
  public Pagereference doSearch()
     {
      results = [select Name,Region__c,Email,LanguageLocaleKey,Manager__c from User Where Region__c  =:User.Region__c];
     
      system.debug('USERSLISTFOR REGION$$$$'+results);
      return null;
     }

 

public List<User> getResults()
    {
        return results;
    }
     
     
   Public Pagereference UpdateManager()
   {
    for(User sluser: selectedusers)
    {
       

     //Update the all the selected users with the  manager id highlighted in the VF page.

 

    }
    return null;
   }
       
    
 }

 

 

 

 

neophyteneophyte

You have two options:

 

1) Use javascript and get the id (or any unique field value) and pass it to the controller as a list. Use this to get the selected records

2)Use a wrapper class with two elements: a boolean variable and a variable of type region. The list displayed will be of this wrapper class. We can identify if a region record is selected by checking the associated boolean variable.

 

Prasanthi BPrasanthi B

Hi,

 

I have used wrapper class to pass the list of selected values to my controller method.

 

But when i'm trying to display the values in the debug log, it is not even entering into the method.

 

It would be more helpful if you can look into my code and suggest.

 

CONTROLLER:

 

public class UserData {
List<Userwrapper> userList {get;set;}
PUBLIC String usr{get;set;}
List<User> results=new List<User>();
public User user;
  public UserData(ApexPages.StandardController controller)
   {
     this.user= (User)controller.getRecord();
   }
    
  public Pagereference doSearch()
     {
      results = [select Name,Region__c,Email,LanguageLocaleKey,Manager__c from User Where Region__c =:User.Region__c];
      system.debug('USERSLISTFOR REGION$$$$'+results);
      return null;
     }
  public List<Userwrapper> XXX() 
  {
     for(User u:results)
      {
        userList.add(new Userwrapper(u));
      }
      system.debug('****ADDEDUSERS****'+userlist);
     return userList;
  }
   // If Updatemanager button is clicked, only the selected users through checkbox should be returned in this method.  
   Public PageReference getSelected()
   {
        system.debug('ENTERED INTO SELECTED');
        List<User> selectedUsers = new List<User>();
        for(userwrapper usrwrapper : userList)
        {
          if(usrwrapper.selected == true)
          {
           selectedusers.add(usrwrapper.wuser);
          }
        }
        system.debug('@@@SELECTEDUSERs@@@:'+selectedusers); 
        return null;
  }
  
   Public Pagereference UpdateManager()
   {
        return null;
   }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }

 

 

VFPAGE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
    
   <apex:inputField id="startMode" value="{!User.Region__c}" />
   <br></br>
      <apex:commandButton action="{!doSearch}" value="Search" rerender="table" >               
   </apex:commandButton>
  <br></br> 
  <apex:pageBlockSection >
   <apex:inputField value="{!User.ManagerId}" id="Manager"/>
  </apex:pageBlockSection> 
    
  <apex:pageBlockTable value="{!results}" var="t" id="table">
        <apex:column >
          <apex:inputCheckbox value="{!User.ManagerId}" id="checkedone">
          </apex:inputCheckbox>
        </apex:column>
        <apex:column value="{!t.Name}"/>
        <apex:column value="{!t.Email}"/>
        <apex:column value="{!t.Region__c}"/>
        <apex:column value="{!t.Manager__c}"/>
      
  </apex:pageBlockTable> 
<!--Whenever 'UpdateManaer' is clicked, the selectec list of values need to be passes to controller method   -->
  <apex:commandButton action="{!getSelected}" value="UpdateManager"  />
  </apex:pageblock>    
  </apex:form>
  </apex:page>