3

I have View page, it has a related list contained within it. However when you use visualforce pages you lose the ability to do some prepopulation with $Action.New, so instead i am trying to get pass a parameter from the original view to the New page, so i can prepopulate a portion of the page.

Is this feasible, currently both the VF pages are using different controllers will i need to make them use the same controller to be able to do so?

2 Answers 2

2

In your View Visualforce page controller your "New" child button can invoke something like:

public PageReference newChild() {
    PageReference pr = Page.NewChild;
    pr.setRedirect(true);
    Map<String, String> p = pr.getParameters();
    p.put('parentId', parent.Id); 
    p.put('parentName', parent.Name);
    p.put('text', parent.text);
    return pr;
}

and in your NewChild Visualforce page controller you can pickup these parameters using:

private void init() {
    Map<String, String> p = ApexPages.CurrentPage().getParameters();
    Id parentId = (Id) p.get('parentId');
    String parentName = p.get('parentName');
    String text = p.get('text');
    // Use the values
}

This way the communication between the pages is via the command-line parameters (automatically encoded) rather than by sharing view state (that can be done by sharing the same controller).

As long as there are just a few parameters and they are reasonably small, the command-line parameter approach provides an efficient and clean way to interface between the two pages, and avoids intertwining their controllers.

2

Similar to the solution here: how to direct a user to the record type selector from a VF page


How to set a URL Parameter in the first controller, suppose you have a customParameter you want to pass to your next VF Page.

string customParameter = 'My Record Type';
pageReference pgRef = new pageReference ('/apex/myVFPage?customParam='+customParameter);

The server reads URL Parameters like this: suppose we have a URL

string URL='https://na15.salesforce.com/setup/own/groupdetail.jsp?id=00Gi0000001joea&setupid=PublicGroups';

The server first takes the string of everything after the ? and splits by & to get a list of Parameters.

list<string> parameterList = URL.substringAfter('?').split('&');

Then you get a map by splitting each parameter by =

map<string,string> parameterMap = new map<string,string>();

for(string parameter:parameterList)
    parameterMap.put(parameter.substringBefore('='),parameter.substringAfter('='));

So the final map in our example equals

parameterMap = new map<string,string>{
         'id' => '00Gi0000001joea',
    'setupid' => 'PublicGroups'
}

To get a URL Parameter in a controller, use the ApexPages.currentPage().getParameters() method to get a map<string,string> of the URL Parameters.

public class Cntrlr_myVFPage{

    public Cntrlr_myVFPage(){
        string recordTypeName = ApexPages.currentPage().getParameters().get('customParam');

        // do something with the data.  what happens if you get a NULL result?
        system.debug(system.LoggingLevel.ERROR,'customParam = '+customParam);
        // output:  customParam = My Record Type
    }

}
1
  • So, its like anyone could be used: 1). URL Re-Writing, 2). Using Session / Cookie concept 3). Hidden form fields using <apex:inputHidden>. And I think point-1 is being explained here.
    – sanchit
    Mar 12, 2014 at 9:42

You must log in to answer this question.

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