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
AbiBrownieAbiBrownie 

Passing values from one visualforce page to another

I use a controller to display a list of products with a checkbox against each product value . When the end user selects few products and clicks "add quantity" button it should navigate to another VF page in which i should display the selected products and an input filed for quantity. Quite similiar to the way of adding products in opportunity. In the code below im using a single controller for 2 VF pages. Problem i m facing here is when the second page is called the wrapperList becomes null instead of holding the values of the selected products. Please give me some suggestions.

 

public with sharing class productAdd
{        
    public  List<Product2> Stdproduct{get;set;}
    public List<wrapper> wrapperList {get;set;}  
    public list<wrapper> SelectedprodList{get;set;}	
    public class wrapper
    {
        public product2 cont{get;set;}
        public Boolean selected{get;set;}
        public integer quantity{get;set;}
        public wrapper(product2 c)
        {
            cont = c;
            selected = false;
        } 
    }
	public  List<wrapper> getProdList()
    {        
        if((wrapperList == null) || (wrapperList.size() == 0))
        {                      
            wrapperList = new List<wrapper>();
            Stdproduct = [Select id, Name from product2 limit 2];           
            for(Product2 c :(List<Product2>)Stdproduct)            
            {               
                wrapperList.add(new wrapper(c));
            }
        }             
        return wrapperList;
    }
    
    public PageReference AddQuantity()
    {   
        PageReference pageRef= new PageReference('/apex/AddQuantity');
        pageRef.setredirect(true);        
        return pageRef;           
    }

    public list<wrapper> getSelectedproducts()
    {
        selectedprodList = new list<wrapper>();      
        for(wrapper cCon:getProdList())
        {            
            if(cCon.selected==true)  
            {                        
                selectedprodList.add(cCon);
            }                           
        }        
        return selectedprodList;           
    }        
}

<!-- 1st Page -->
<apex:page Controller="productAdd" sidebar="false">
    <table  width="90%">
	    <apex:form>
			<apex:repeat value="{!ProdList}" var="l" >               
				<tr>
				<td>
				<apex:inputCheckbox value="{!l.selected}"/>
				</td>
				<td class="fieldname">{!l.cont.name}</td>                       
				</tr>                
			</apex:repeat>
				<tr>
				<apex:commandButton value="Add Quantity" action="{!AddQuantity}"/>
				</tr>             
	    </apex:form> 
    </table>
</apex:page>


<!-- 2nd Page -->
<apex:page Controller="productAdd">
	<apex:form >
		<apex:pageBlock > 
			<apex:pageBlockTable value="{!Selectedproducts}" var="c" id="table"> 
				<apex:column value="{!c.cont.Name}" />                
				<apex:column >
				<apex:inputText value="{!c.quantity}"/>
				</apex:column> 
			</apex:pageBlockTable> 
		</apex:pageBlock> 
	</apex:form> 
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Sam27Sam27

I think you should do something like -

 

 {   
        PageReference pageRef= new PageReference('/apex/AddQuantity');
        pageRef.setRedirect(false);        
        return pageRef;           
    }

 Actually setRedirect(true) will invoke a new controller for that page hence the list gets null, Hence setRedirect(false) will use the existing controller. I too was in same situation once, Hope this helps.

 

Regards

All Answers

sfdcfoxsfdcfox

You need to make sure you're using the exact same controller and extensions for both pages, or Visualforce won't automatically pass its view state between the pages. Of course, there are other methods of transferring data between pages, but you'll find it far more convenient to just use the same controller for each page.

Sam27Sam27

I think you should do something like -

 

 {   
        PageReference pageRef= new PageReference('/apex/AddQuantity');
        pageRef.setRedirect(false);        
        return pageRef;           
    }

 Actually setRedirect(true) will invoke a new controller for that page hence the list gets null, Hence setRedirect(false) will use the existing controller. I too was in same situation once, Hope this helps.

 

Regards

This was selected as the best answer
AbiBrownieAbiBrownie

Thanks for ur solution.. Its working fine.:)

riffindusriffindus

Hi,

 

I like your solution. Even it worked for me.

 

But this will not work if i use different controller or controller extension also.

 

in very complex app, we will be using more controllers. how we can handle values

 

Any solution?