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
jucuzoglujucuzoglu 

How do I get the objects that have been selected to my APEX controller?

I have a Visualforce page that displays a number of records in a data table with a checkbox in each row. As can be seen in the code below

 

<apex:page controller="ProgramSupportList">
    <apex:pageBlock >
      <apex:form >  
        <apex:pageBlockTable value="{!ProgramSupportItems}" var="psi">
            <apex:column >
                <apex:facet name="header">
                    Select
                </apex:facet>
                <apex:inputCheckbox value="{!psi.id}" />
            </apex:column>        
            <apex:column value="{!psi.id}"/>
            <apex:column value="{!psi.Status__c}"/>
            <apex:column value="{!psi.Date__c}"/>
            <apex:column value="{!psi.Hours__c}"/>
            <apex:column value="{!psi.Program__r.Executive_Coach__c}"/>            
        </apex:pageBlockTable>
        <apex:commandButton action="{!ProcessSupportItems}" value="Invoice Selected Items" />
      </apex:form> 
    </apex:pageBlock>
</apex:page>

 

What I don't know is how to set up the ProcessSupportItems method to grab those values and pass them to a static method.

 

Here is my controller code 

 

public class ProgramSupportList {

    public PageReference ProcessSupportItems() {
         List<Program_Support__c> ml = setCon.getSelected();
         System.Debug('************1');
         System.Debug(ml);
         System.Debug('************2');       
        return null;
    }

    
  // ApexPages.StandardSetController must be instantiated 
    
  // for standard list controllers 
    
    public ApexPages.StandardSetController setCon {
        get {            
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                       [Select Id,Status__c,Program__r.Executive_Coach__c,Date__c, Hours__c From Program_Support__c
                        where Program__r.Executive_Coach__c = '003V0000001y6nC' 
                        AND Status__c != 'Invoiced'
                       ]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records 
    
    public List<Program_Support__c> getProgramSupportItems() {
         return (List<Program_Support__c>) setCon.getRecords();
    }
}

 

I have noticed that the button does not seem to be calling the method as I don't see any of my debug statements. So that may be the issue.

 

NOTE: I eventually did resolve the problem using the wrapper based solution. One thing that was causing a problem for me was also this line of code:

 

<apex:inputCheckbox value="{!psi.id}" />

While it did not generate an error it would not allow any command buttons to properly call the APEX methods. Turns out its not a good idea to bind a checkbox to a read only field as I did in this case :) .I fixed this using code similar to what was presented below in the solution by incorporating the wrapper and binding the checkbox to a boolean field.

Best Answer chosen by Admin (Salesforce Developers) 
Dirk GronertDirk Gronert

You have to wrap your Program_Support__c objects ... pls find below the example how to (i wrote the code down without testing it - excuse for typos!)

 

public class ProgramSupportList{

 

private List<ProgramSupportWrapper> psws {get; set;}

 

        [...]

 

       public List<Program_Support__c> getProgramSupportItems() {

   wrapProgramSupportList();

                   return this.psws;

       }

 

 

    public PageReference ProcessSupportItems() {

List<System_Support__c> selectedProgramSupportObj = new List<System_Support__c>();

for(ProgramSupportWrapper psw : psws){

 selectedProgramSupportObj.add(psw.ps);

}

// now you will have all selected obj in the list and you can do what ever you want!

return null;

   }


private wrapProgramSupportList(){

List<Program_Support__c> pss = (List<Program_Support__c>) setCon.getRecords();

this.psws = new List<ProgramSupportWrapper>();

for(Program_Support__c ps : pss){

psws.add(new ProgramSupportWrapper(ps));

}

}

 

public class ProgramSupportWrapper{

public Program_Support__c ps {get; set;}

public Boolean selected {get;set;}

 

public ProgrammSupportWrapper(Program_Support__c ps){

this.ps = ps;

this.selected = false;

}

}

}

 

 

Than you VF:

 

<apex:page controller="ProgramSupportList">

    <apex:pageBlock >

      <apex:form >  

        <apex:pageBlockTable value="{!ProgramSupportItems}" var="psi">

            <apex:column >

                <apex:facet name="header">

                    Select

                </apex:facet>

                <apex:inputCheckbox value="{!psi.selected}" />

            </apex:column>        

            <apex:column value="{!psi.ps.id}"/>

            <apex:column value="{!psi.ps.Status__c}"/>

            <apex:column value="{!psi.ps.Date__c}"/>

            <apex:column value="{!psi.ps.Hours__c}"/>

            <apex:column value="{!psi.ps.Program__r.Executive_Coach__c}"/>            

        </apex:pageBlockTable>

        <apex:commandButton action="{!ProcessSupportItems}" value="Invoice Selected Items" />

      </apex:form> 

    </apex:pageBlock>

</apex:page>

 

Cheers,

--dirk

All Answers

Starz26Starz26

You will either have to use a wrapper class to display the rows or utilize a standardsetcontroller class to display the rows and utilize the built in selection functionality..

 

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardsetcontroller.htm

dmchengdmcheng

Have you considered using a StandardSetController?  That will provide you with the checkboxes and selection functionality through getSelected.

jucuzoglujucuzoglu

I am trying to use the method your describing, but not having much luck. I have my posted my controller code.

dmchengdmcheng

I'm stumped.  I see you don't have a constructor in your controller code but I don't think that matters.  I also see you haven't cast the getSelected(), but it seems like casting is not required in most cases since I've seen numerous examples of uncast getRecords() and getSelected().

Starz26Starz26

I beleive you have to rerender something to get the command button to wkr? I recall reading that somewhere...maybe one of Bob's blog posts

 

Adding an <apex;pageblock id='hiddenBlock' rendered='false'/>

 

then add rerender='hiddenBlock' to the command button and I believe that should work..

 

 

Or it could be the need to use immediate='true'

 

try each one or both...

Dirk GronertDirk Gronert

You have to wrap your Program_Support__c objects ... pls find below the example how to (i wrote the code down without testing it - excuse for typos!)

 

public class ProgramSupportList{

 

private List<ProgramSupportWrapper> psws {get; set;}

 

        [...]

 

       public List<Program_Support__c> getProgramSupportItems() {

   wrapProgramSupportList();

                   return this.psws;

       }

 

 

    public PageReference ProcessSupportItems() {

List<System_Support__c> selectedProgramSupportObj = new List<System_Support__c>();

for(ProgramSupportWrapper psw : psws){

 selectedProgramSupportObj.add(psw.ps);

}

// now you will have all selected obj in the list and you can do what ever you want!

return null;

   }


private wrapProgramSupportList(){

List<Program_Support__c> pss = (List<Program_Support__c>) setCon.getRecords();

this.psws = new List<ProgramSupportWrapper>();

for(Program_Support__c ps : pss){

psws.add(new ProgramSupportWrapper(ps));

}

}

 

public class ProgramSupportWrapper{

public Program_Support__c ps {get; set;}

public Boolean selected {get;set;}

 

public ProgrammSupportWrapper(Program_Support__c ps){

this.ps = ps;

this.selected = false;

}

}

}

 

 

Than you VF:

 

<apex:page controller="ProgramSupportList">

    <apex:pageBlock >

      <apex:form >  

        <apex:pageBlockTable value="{!ProgramSupportItems}" var="psi">

            <apex:column >

                <apex:facet name="header">

                    Select

                </apex:facet>

                <apex:inputCheckbox value="{!psi.selected}" />

            </apex:column>        

            <apex:column value="{!psi.ps.id}"/>

            <apex:column value="{!psi.ps.Status__c}"/>

            <apex:column value="{!psi.ps.Date__c}"/>

            <apex:column value="{!psi.ps.Hours__c}"/>

            <apex:column value="{!psi.ps.Program__r.Executive_Coach__c}"/>            

        </apex:pageBlockTable>

        <apex:commandButton action="{!ProcessSupportItems}" value="Invoice Selected Items" />

      </apex:form> 

    </apex:pageBlock>

</apex:page>

 

Cheers,

--dirk

This was selected as the best answer
Dirk GronertDirk Gronert

or you can use the list functionality on the object tab --> select some records and open your vf to do something with the selected records ...

 

see also this post:

http://boards.developerforce.com/t5/Visualforce-Development/How-to-mimic-the-list-functionality-in-VisualForce/td-p/399989

 

cheers,

--dirk