2

I am trying to pass list from One controller to another controller , but i don't know how to pass the list.

Controller 1

public PageReference SendEmail()
     {
        ListSelectedOffer =new List<Offer__c>();
        for(OfferWrapperClass offerWrapper:OfferWrapperList){
          if(offerWrapper.isSelected==true){
                    ListSelectedOffer.add(offerWrapper.Offer.id); // here is the list which i want to pass to another controller
           }

       }

       system.debug('Selected Offer ------->'+ListSelectedOffer.size());


        PageReference orderPage = new PageReference('/apex/offerEmail?id=' +loanId);
        orderPage.setRedirect(true);
        orderPage.getParameters().put('SelectedList',String.valueOf(ListSelectedOffer));
        return orderPage;

     }

Controller 2

public OfferSentEmail(ApexPages.StandardController controller) {
        OfferInformation = controller.getRecord().Id;
        Loan = [Select id, Name from fintechLLC__Loan__c where id = : OfferInformation];
        AllOffers = new List < offer__c > ();
        ToAddresses = new List < String > ();
        Offer= Offer.valueOf((ApexPages.currentPage().getParameters().get('SelectedList')));
        OfferID=new List<String>();
        OfferID.add(Offer.id);
        fetchtAllRelatedLoanOffers();

    }
1
  • 1
    Can't you use same controller for both VF page ? that will be easier to share data between two VF page..
    – Ratan Paul
    Commented Mar 1, 2016 at 13:13

2 Answers 2

3

Normally if you wanted to pass the state between two Visualforce pages, you would need to make a stateful post-back request. This requires that the two pages

  • Share the same controller.
  • Navigate using HTTP POST (not GET)
  • You have a non-transient variable or property in the controller to store the list

So first my controller:

public static SharedStateCtrl {

  public List<Id> ListSelectedOffer {get;set;}

  ...

  public PageReference sendEmail(){

  ...

  //no setRedirect=True, this makes it a GET request      
  //now you've set the list into the view state
  return orderPage;

While this may seem like magic, it is just taking advantage of the ViewState features of Visualforce.

2

If the maximum number of selected offers is limited (say 100 or less) then you can pass the ID values in the URL. (The maximum length of a URL can be limited by e.g. proxy servers). Here is how to do that separating the ID values with commas.

First page passing the ID values:

Id[] items = ...;
...
PageReference orderPage = ...
orderPage.getParameters().put('items', String.join(items, ','));
...

Second page picking them up:

Id[] items = ApexPages.currentPage().getParameters().get('items').split(',');

This approach avoids coupling what may be quite different pages to a common controller.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .