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
s.mafoders.mafoder 

Passing a list of ids from one visualforce to another

Hello , 

 

I have a visualforce page that let a user select many records and then the user should click on a button to go to another page to update records selected (its like the add prodcut in an opportunity). 

 

The problem am facing , is that i'm able to get ids of elements selected , but i dont know how to pass the list of ids to the other page...

 

Here is the visualforce page : 

 

<apex:page controller="CCW_ContactPaginationController2">
 
    <script type="text/javascript">
 
        /*
        *    function to handle checkbox selection
        */
        function doCheckboxChange(cb,itemId){
 
            if(cb.checked==true){
                aSelectItem(itemId);
            }
            else{
                aDeselectItem(itemId);
            }
 
        }
 
    </script>
 
    <apex:form >
 <apex:commandbutton value="Update selected records" action="{!updateselected}"/>
        <!-- handle selected item -->
        <apex:actionFunction name="aSelectItem" action="{!doSelectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>
 
        <!-- handle deselected item -->
        <apex:actionFunction name="aDeselectItem" action="{!doDeselectItem}" rerender="mpb">
            <apex:param name="contextItem" value="" assignTo="{!contextItem}"/>
        </apex:actionFunction>
 
        <apex:pageBlock title="CloudClickware Pagination Sample" id="mpb">
 
            <!-- table of data -->
            <apex:pageBlockTable title="Contacts" value="{!contacts}" var="c">
                <apex:column >
                    <apex:facet name="header">Action</apex:facet>
                    <apex:inputCheckbox value="{!c.IsSelected}" onchange="doCheckboxChange(this,'{!c.tContact.Id}')"/>
                </apex:column>
                <apex:column value="{!c.tContact.FirstName}"/>
                <apex:column value="{!c.tContact.LastName}"/>
                <apex:column value="{!c.tContact.Title}"/>
                <apex:column value="{!c.tContact.Phone}"/>
                <apex:column value="{!c.tContact.Email}"/>
            </apex:pageBlockTable>
 
            <!-- count of selected items -->
            <apex:outputLabel value="[{!selectedCount} records selected]" />
 
            <br/>
 
            <!-- next, previous and page info -->
            <apex:commandLink action="{!doPrevious}" rendered="{!hasPrevious}" value="Previous" />
            <apex:outputLabel rendered="{!NOT(hasPrevious)}" value="Previous" />
 
            <apex:outputLabel value=" (page {!pageNumber} of {!totalPages}) " />
 
            <apex:commandLink action="{!doNext}" rendered="{!hasNext}" value="Next" />
            <apex:outputLabel rendered="{!NOT(hasNext)}" value="Next" />
 
        </apex:pageBlock>
 
    </apex:form>
 
</apex:page>

 And the controler class :

 

 

public with sharing class CCW_ContactPaginationController2 {
 
    /*
    *   item in context from the page
    */
    public String contextItem{get;set;}
 
    /*
    *   set controller
    */
    private ApexPages.StandardSetController setCon;
 
    /*
    *   the contact ids selected by the user
    */
    private Set<Id> selectedContactIds;
 
    /*
    *   constructor
    */
    public CCW_ContactPaginationController2 ()
    {
        //init variable
        this.selectedContactIds= new Set<Id>();
 
        //gather data set
        this.setCon= new ApexPages.StandardSetController( [SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact] );
        this.setCon.setpageNumber(1);
        this.setCon.setPageSize(10);
 
    }
 
    /*
    *   handle item selected
    */
    public void doSelectItem(){
 
        this.selectedContactIds.add(this.contextItem);
 
    }
 
    /*
    *   handle item deselected
    */
    public void doDeselectItem(){
 
        this.selectedContactIds.remove(this.contextItem);
 
    }
 
    /*
    *   return count of selected items
    */
    public Integer getSelectedCount(){
 
        return this.selectedContactIds.size();
 
    }
    public Pagereference updateselected()
    {
    
       return null;
    
    }
 
    /*
    *   advance to next page
    */
    public void doNext(){
 
        if(this.setCon.getHasNext())
            this.setCon.next();
 
    }
 
    /*
    *   advance to previous page
    */
    public void doPrevious(){
 
        if(this.setCon.getHasPrevious())
            this.setCon.previous();
 
    }
 
    /*
    *   return current page of groups
    */
    public List<CCWRowItem> getContacts(){
 
        List<CCWRowItem> rows = new List<CCWRowItem>();
 
        for(sObject r : this.setCon.getRecords()){
            Contact c = (Contact)r;
 
            CCWRowItem row = new CCWRowItem(c,false);
            if(this.selectedContactIds.contains(c.Id)){
                row.IsSelected=true;
            }
            else{
                row.IsSelected=false;
            }
            rows.add(row);
        }
 
        return rows;
 
    }
 
    /*
    *   return whether previous page exists
    */
    public Boolean getHasPrevious(){
 
        return this.setCon.getHasPrevious();
 
    }
 
    /*
    *   return whether next page exists
    */
    public Boolean getHasNext(){
 
        return this.setCon.getHasNext();
 
    }
 
    /*
    *   return page number
    */
    public Integer getPageNumber(){
 
        return this.setCon.getPageNumber();
 
    }
 
    /*
    *    return total pages
    */
    Public Integer getTotalPages(){
 
        Decimal totalSize = this.setCon.getResultSize();
        Decimal pageSize = this.setCon.getPageSize();
 
        Decimal pages = totalSize/pageSize;
 
        return (Integer)pages.round(System.RoundingMode.CEILING);
    }
 
    /*
    *   helper class that represents a row
    */
    public with sharing class CCWRowItem{
 
        public Contact tContact{get;set;}
        public Boolean IsSelected{get;set;}
 
        public CCWRowItem(Contact c, Boolean s){
            this.tContact=c;
            this.IsSelected=s;
        }
 
    }
}

 

So how can i implement this in that method :

 

    public Pagereference updateselected()
    {
    
       return null;
    
    }

So i can pass selected ids in Page2
Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

I agree with @avidev9, take a look at the example for a CustomController wizard in the VF Developer's Guide.  You use the controller's viewstate to maintain the list of ids from page 1 to use in page2's action methods

 

Other options are ugly -

 

* using Apexpages getparameters().put('idList',yourIdListAsDelimitedString) - this would only work if you use a few ids

* saving the list of ids in a list of children Sobjects to the page 1 SObject - and then passing the id of the page 1 Sobject to page 2 that then fetches the children SObjects to find the necessary ids and once finished with any DML, deletes those children -- yuck

All Answers

Avidev9Avidev9

Simple solution would be keep the COntroller for both the pages same.

In this way when you navigate to PAGE2 the values in the list wont be lost and you can access them.

crop1645crop1645

I agree with @avidev9, take a look at the example for a CustomController wizard in the VF Developer's Guide.  You use the controller's viewstate to maintain the list of ids from page 1 to use in page2's action methods

 

Other options are ugly -

 

* using Apexpages getparameters().put('idList',yourIdListAsDelimitedString) - this would only work if you use a few ids

* saving the list of ids in a list of children Sobjects to the page 1 SObject - and then passing the id of the page 1 Sobject to page 2 that then fetches the children SObjects to find the necessary ids and once finished with any DML, deletes those children -- yuck

This was selected as the best answer
s.mafoders.mafoder

Hello , 

 

I tried it and it works ! Thanks

vanessenvanessen
In the doc it says that only variable that is used on the first page is kept in the viewstate. But i have to process this list and send it to another page. How can i achieve this via wizard? i know that for a field value, i can use an input hidden, but for a list?