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
Ali Husain 3Ali Husain 3 

Passing selected values from one VF page to another

I have a custom object Deal_Reg__c. The business flow for this object is that a user can go to this object's page and add a Product to this Deal Reg. I have created a junction object called Deal_Reg_Products__c that has a Deg Reg Lookup as well as a Product Lookup along with other custom fields. After you press the "Add" button on the Deal Reg Products Related List section of the Deal Reg page, i would like the first VF page to be displayed.

The first VF page should basically only be a selectable list of all the Products. After the user selects one or multiple records to add to the specific Deal Reg, they will press a button called "Add Product". Pressing that button should fire off the second VF page.

The second VF page should consist of the selected list of Products from the first VF page along with a Deal_Reg_Products__c custom field, "Amount", next to each record where the user can input the value. After a value has been entered for each Product, the user will press a "Confirm" button which will send us back to the Deal Reg page where in the Deal Reg Products Related List section the selected Products are now visible and tied to that Deal Reg.

1. How to I tie this to the specific Deal Reg that the user would like to add the Products to?
2. After selecting the Products, how do I bring those values over to the second VF page where i can then enter in an "Amount" for each?
3. How can I get the "Confirm" button to actually commit the Products to the Deal Reg?

Here is the code i currently have:

VF Page 1:

<apex:page standardController="Deal_Registration_Products__c" extensions="wrapperClassController" tabstyle="Deal_Reg__c" id="myPage" recordSetVar="dealregproducts">
    <apex:form id="myForm">
        <apex:pageBlock title="Find Products" id="mypageBlock">
            <table width="100%">
                <tr>
                    <td width="25%" align="right">
                        <apex:pageBlockSection columns="1">
                            <b>By Keyword </b><br/>
                            <apex:inputText value="{!inputText1}" html-placeholder="Product Name"/>
                            <apex:commandButton value="Search" action="{!searchProduct}"/>  
                        </apex:pageBlockSection>
                    </td>
                </tr>
            </table>
            <br/>
            <p/>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Products" action="{!DisplayDetails}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cProduct records -->
            <apex:pageBlockTable value="{!products}" var="p" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!p.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cProduct container/wrapper -->
                <apex:column value="{!p.prod.Name}" />
                <apex:column value="{!p.prod.ProductCode}" />
                <apex:column value="{!p.prod.Description}" />
                <apex:column value="{!p.prod.Family}" />
            </apex:pageBlockTable>
            <apex:outputLabel >{!SelectedProd}</apex:outputLabel>
        </apex:pageBlock>
    </apex:form>
</apex:page>


VF Page 2:

<apex:page standardController="Deal_Registration_Products__c" extensions="wrapperClassController">
   <apex:form >
       <apex:pageBlock title="Deal Registration Products">
            <apex:pageBlockButtons >
                <apex:commandButton value="Confirm"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!SelectProdDetails}" var="p">
                <apex:column value="{!p.prod.Name}"/>
                <!-- <apex:column value="{!p.prod.Forecast_Amount__c}"/> -->
            </apex:pageBlockTable>
            <apex:outputPanel >
                <apex:outputLabel >{!SelectProdDetails}</apex:outputLabel>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>   
</apex:page>


Controller (I apologize for the mess here since I've been trying many different things):

public with sharing class wrapperClassController {

    public wrapperClassController(ApexPages.StandardSetController controller) {

    }


    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
           
                    setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id,Name,ProductCode,Description,Family from Product2 Order by Name]));
            }
            system.debug('setConSize-----'+setCon.getResultSize());
            return setCon;
        }
        set;
    }
    
    public wrapperClassController(ApexPages.StandardController controller) {

    }

   
    public String inputText1 {get;set;}
    public String productName {get;set;}
    public Boolean isListEmpty {get;set;}
    public List<cProduct2> productWrapperlist = new List<cProduct2>();
    public List<List<cProduct2>> listOfProductWrapperlist = new List<List<cProduct2>>();
   
    //Our collection of the class/wrapper objects cProduct2
    public List<cProduct2> productList {get; set;}
    public List<cProduct2> selectedproductList {get; set;}
    public List<Product2> selectedProducts {get; set;}
    public List<Product2> selectedProd = new List<Product2>();

    //This method uses a simple SOQL query to return a List of Products
    public List<cProduct2> getProducts() {
        if(productList == null) {
            productList = new List<cProduct2>();
            for(Product2 p: [select Id, Name, ProductCode, Description, Family from Product2 limit 100]) {
                // As each product is processed we create a new cProduct2 object and add it to the productList
                productList.add(new cProduct2(p));
            }
        }
        return productList;
    }


    public void processSelected() {

        //We create a new list of Products that we be populated only with Products if they are selected
        //List<Product2>
        selectedProducts = new List<Product2>();

        //We will cycle through our list of cProducts and will check to see if the selected property is set to true, if it is we add the Product to the selectedProducts list
        for(cProduct2 cProd: getProducts()) {
            if(cProd.selected == true) {
                selectedProducts.add(cProd.prod);
            }
        }

        // Now we have our list of selected products and can perform any type of logic we want, sending emails, updating a field on the Product, etc
        //System.debug('These are the selected Products...');
       // for(Product2 prod: selectedProducts) {
       //     system.debug(prod);
       // }
       // productList=null; // we need this line if we performed a write operation  because getProducts gets a fresh list now
       // return null;
    }


    public List<cProduct2> getdealregProductsDetails()
    {
        //If there are records to be displayed then show the records else display message
        if((setCon.getResultSize()) > 0)
        {
            if(listOfProductWrapperlist[setCon.getPageNumber()-1].size() == 0) {
                productWrapperlist = new List<cProduct2>();
                for(Product2 p : (List<Product2>)setCon.getRecords()) {
                    productWrapperlist.add(new cProduct2(p));
                }
                listOfProductWrapperlist[setCon.getPageNumber()-1] = productWrapperlist;
            }
            system.debug('productWrapperlist-----'+productWrapperlist);
            return listOfProductWrapperlist[setCon.getPageNumber()-1];
        }
        else
        {
            return null;
        }          
    }
    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cProduct2 {
        public Product2 prod {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cProduct2 object we pass a Product that is set to the con property. We also set the selected value to false
        public cProduct2(Product2 p) {
            prod = p;
            selected = false;
        }
       
        public cProduct2(Product2 prd, boolean check) {
            prod = prd;
            selected = check;
        }
    }
   
   
    public void searchProduct() {
            system.debug('productName : '+productName);
            if(productName != null && productName != '') {
                setCon = new ApexPages.StandardSetController(Database.query('Select Id,Name,ProductCode,Description,Family from Product2 where Name like \'%'+productName+'%\' Order by Name limit 2000'));
               
            } else {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id,Name,ProductCode,Description,Family from Product2 Order by Name]));
            }
            listOfProductWrapperlist.clear();
            setCon.setPageSize(25);
            Integer reminder = math.mod(setCon.getResultSize(),setCon.getPageSize());
            Integer listToCreateSize = (reminder == 0?(setCon.getResultSize()/setCon.getPageSize()):(setCon.getResultSize()/setCon.getPageSize())+1);
            for(Integer i=0; i < listToCreateSize ; i++){
                listOfProductWrapperlist.add(new List<cProduct2>());
            }
            if(listOfProductWrapperlist== null || listOfProductWrapperlist.isEmpty()) {
                isListEmpty = true;
            }      
        }
       
    public PageReference getForecastAmount() {
        Page.DealRegProducts.getParameters().put('products_selected', 'selectedProducts');
        //processSelected();
        return Page.DealRegProducts;
    }
   
    //public PageReference secondTry() {
    //    PageReference page = Page.DealRegProducts;
    //    page.getParameters().put(Id, selectedProducts.Id);
    //    return page;
    //}
   
    public List<Product2> getSelectedProd() {
        if(selectedProd.size()>0) {
            System.debug(' selectProd '+selectedProd);
            return selectedProd;
        }
        else
        return null;
    }
   
    public PageReference DisplayDetails() {
        selectedproductList = new List<cProduct2>();
       
        for(cProduct2 prd : productList) {
            if(prd.selected == true) {
                selectedproductList.add(prd);
            }
        }
       
        PageReference reRend = new PageReference('/apex/DealRegProducts');
        reRend.setRedirect(false);
        return reRend;
    }
   
    public List<cProduct2> getProdDetails() {
        productList = new List<cProduct2>();
        List<Product2> prods = [Select Id,Name,ProductCode,Description,Family from Product2 Order by Name];
       
        for(Product2 p : prods) {
            productList.add(new cProduct2(p, false));
        }
        return productList;
    }
   
    public List<cProduct2> getSelectProdDetails() {
        selectedproductList = new List<cProduct2>();
       
        for(cProduct2 c : getProdDetails()) {
            System.debug(' selectProdDetails ' + c);
           
            if(c.selected == true)
            selectedproductList.add(c);
        }
        return selectedproductList;
    }
}
Tushar sharmaTushar sharma
you can task example from Wizard to passs data from ne VF apg eto another you can check here
https://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_wizard.htm
Ali Husain 3Ali Husain 3
Thanks for the response!

How would I pass selected records from one VF page to another?