[SalesForce] How to move selected check box rows from one vf to another

enter image description hereI am trying to move my selected Checkbox rows from one vf page to another, for that i am adding all selected item in a list and then moving to another vf page since i am using the same controller the value of that list should be available there but i am not getting anything please someone help me out.

My 1st vf page code–

<apex:page tabStyle="Opportunity" controller="GEN_ProductSelectionController" >
  <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
   </script> 

  <apex:form >
    <apex:pageBlock title="Find Product" >
     <apex:pageBlockButtons Location="Top" >
         <apex:commandButton value="Select" action="{!closeProduct}"/>
         <apex:commandButton value="Cancel" action="{!Reset}"/>
     </apex:pageBlockButtons>
     <h1>By Keyword</h1>
      <apex:pageBlockSection >
        <apex:inputText value="{!searchstring}"/> 
      </apex:pageBlockSection>
      <apex:commandButton value="Search records" action="{!search}" />
    </apex:pageBlock>

    <apex:pageBlock >


                <apex:pageBlockTable value="{!wrapProductList}" var="accWrap"   >
                    <apex:column >
                      <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                      <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column >  
                    <apex:outputlink value="https://ap1.salesforce.com/{!accWrap.pro.id}">{!accWrap.pro.Name}</apex:outputlink>  
                    </apex:column> 
                    <apex:column value="{!accWrap.pro.ProductCode}" />
                    <apex:column value="{!accWrap.pro.Description}" />
                    <apex:column value="{!accWrap.pro.Family}" />
                    <apex:column value="{!accWrap.pro.Type__c}" />

                    </apex:pageBlockTable>


        </apex:pageBlock>
  </apex:form>
</apex:page>

Controller–

public class GEN_ProductSelectionController {


     ID cid = ApexPages.currentPage().getParameters().get('id');

     public PageReference Reset() {
            return null;
     }


     public List<wrapProduct> wrapProductList {get; set;}
     public List<Product2> selectedProducts{get;set;}
     public List<OpportunityLineItem> closeProduct{get;set;}
     public List <Product2> pro {get;set;}  
     public String searchstring {get;set;}  

     public void search(){  
             wrapProductList = new List<wrapProduct>();
             string searchquery='select name,id,ProductCode,Description,Family,Type__c from Product2 where name like \'%'+searchstring+'%\' Limit 20';  

             for(Product2 a: Database.query(searchquery)) {
                  wrapProductList.add(new wrapProduct(a));
             }
     }  

    public GEN_ProductSelectionController(){
             wrapProductList = new List<wrapProduct>();

             for(Product2 a: [select Id, Name,ProductCode,Description,Family,Type__c from Product2]) {
             wrapProductList.add(new wrapProduct(a));
             }
    }

    public class wrapProduct {
            public Product2 pro {get; set;}
            public Boolean selected {get; set;}

            public wrapProduct(Product2 p) {
                pro = p;
                selected = false;
            }
    }

    public PageReference closeProduct() {
            selectedProducts = new List<Product2>();

            for(wrapProduct wrapProductObj : wrapProductList) {

                 if(wrapProductObj.selected == true) {
                  selectedProducts.add(wrapProductObj.pro);
                 }
            }
            PageReference pagePrice = new PageReference('/apex/GEN_MultiEdit?id='+cid);
            pageprice.setRedirect(true);
            return pageprice; 

    }

    public void closeAccou(){
            selectedProducts = new List<Product2>();

            for(wrapProduct wrapProductObj : wrapProductList) {

               if(wrapProductObj.selected == true) {
                   selectedProducts.add(wrapProductObj.pro);
               }
            }
              insert selectedProducts;


    }



}

Now my next vf page where i want to show that selected selected products GEN_MultiEdit

<apex:page tabStyle="Opportunity" controller="GEN_ProductSelectionController" >
 <apex:form >
 <apex:pageBlock >
<apex:pageBlockTable value="{!selectedProducts}" var="c" title="Selected Products">
<apex:column value="{!c.Name}" headerValue="Product Name"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Here i am trying to get those products name

enter image description here

Best Answer

I believe that your issue is that you're setting pageprice.setRedirect(true); in your closeProduct() method.

Setting the redirect to true causes Salesforce to issue a completely new HTTP GET request. This redirect process also flushes the viewstate, which contains the information used to reconstruct the state of your controller.

This means that with setRedirect(true), all your information is lost unless you pass the information through a page parameter (or save it in an sObject and then query for the information in your controller's constructor).

remove pageprice.setRedirect(true);, and your second page should work as you expect.

Related Topic