2
public PageReference BookerInformation(){

        PageReference gotosetup = new PageReference('/apex/bookerinformation?id=' + eventId);

        List<ProntoEvents_Product__c> products_1 = [SELECT Id, Name FROM ProntoEvents_Product__c Where Event_Name__c = :eventId];
        Map<Id, String> prod_quantity = new Map<Id, String>();  
        for (ProntoEvents_Product__c data_prod : products_1){
            prod_quantity.put(data_prod.ID, ApexPages.currentPage().getParameters().get(data_prod.ID));

        }

        system.Debug(prod_quantity);
        gotosetup.setRedirect(true);
        return gotosetup;
    }

How can I pass this prod_quantity to a global variable so that I can use it on the visualforce page?

Sample output of the prod_quantity in system.Debug is this:

16:54:11.341 (341390959)|USER_DEBUG|[50]|DEBUG|{a0T9000000JplalEAB=15, a0T9000000Jple3EAB=0, a0T9000000JplfLEAR=5}
1
  • if your application can be more like a wizard, then by not using setRedirect(true), the controller variables stay in memory for both pages. Otherwise, KeithC has the right answer
    – cropredy
    Commented Jul 11, 2015 at 17:55

1 Answer 1

1

It looks like you are trying to make prod_quantity available to the next page, the bookerinformation page.

There is nowhere (except the database) at the moment in Salesforce to keep a value created in one page and make it available to another page. So the normal solution to your problem is to pass the value in the URL of the new page just as the eventId is being passed in the URL.

Providing your data is not too large (doesn't make the URL more than say 1k in length) you can encode and decode it into the URL like this:

public PageReference BookerInformation() {

    ...

    Map<Id, String> prod_quantity = ...;
    String encoded = EncodingUtil.urlEncode(JSON.serialize(prod_quantity), 'UTF-8')

    PageReference pr = new PageReference('/apex/bookerinformation');
    pr.getParamaters().put('id', eventId);
    pr.getParamaters().put('prod_quantity', encoded);
    pr.setRedirect(true);
    return pr;
}

and the new page can get the value like this:

String encoded = ApexPages.currentPage().getParameters().get('prod_quantity');
String decoded = EncodingUtil.urlDecode(encoded, 'UTF-8');
Map<Id, String> prod_quantity
        = (Map<Id, String>) JSON.deserialize(decoded, Map<Id, String>.class);

But generally it is better to find a way to pass a small identifier that the receiving page can use to query the data it needs.

2
  • If he's using the same controller for both pages, would .setRedirect(false) keep the variable for use in the next page? Commented Dec 30, 2015 at 21:06
  • @AntonioManente Yes if prod_quantity was also made a (non-static and non-transient) field of the class rather than a local variable that will not be preserved in the view state.
    – Keith C
    Commented Dec 30, 2015 at 23:29

You must log in to answer this question.

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