2

On a custom VF page (call it page1), I am using apex:commandLink to link to another custom VF page (call it page2) and perform a DML statement. I need to put a series of parameters into the URL as the page I am linking to (page2) uses the parameters to load the appropriate data. Here is my current code:

<apex:commandLink styleClass="button" action="{!saveNextResponse}" value="Save and Continue">
</apex:commandLink>

public String nextQuestionId {get;set;}
public String filter {get; set;}
public String questionBankId {get; set;}

nextQuestionId = '123';
filter = 'all';
questionBankId = '456';

public PageReference saveNextResponse(){
    //doing a DML as well
    pageRef = new PageReference('/apex/Question');
    pageRef.getParameters().put('question',nextQuestionId);
    pageRef.getParameters().put('filter',filter);
    pageRef.getParameters().put('questionBank',questionBankId);

    return pageRef;
}

As I currently have it setup, the DML is working and page2 is loading as expected; however the actual URL itself does not contain the parameters. Instead of being "/Question?question=123&filter=all&questionBank=456", it's just "/Question". What can I do to ensure the parameters are in the URL itself?

  • pageRef = new PageReference('/apex/Question?question=' + question + '&filter=' + filter + '&questionBank=' + questionBank); – battery.cord Sep 29 '15 at 15:41
8

You have to use setRedirect(true); in your controller as below. Hope this helps.

public PageReference saveNextResponse(){
    //doing a DML as well
    pageRef = new PageReference('/apex/Question');
    pageRef.setRedirect(true);
    pageRef.getParameters().put('question',nextQuestionId);
    pageRef.getParameters().put('filter',filter);
    pageRef.getParameters().put('questionBank',questionBankId);
    return pageRef;
}
  • @john_yankee Can you please mark it as answer. So it will help others to know that this answers the question. – Saroj Bera Sep 29 '15 at 16:16
0

What you have appears to be correct. Adding parameters via getParameters().put('key', 'value') should show up in the query string on your url.

The only case I can think of where query string parameters you've added wouldn't show up is if the variable you're using to set the parameter value is null. (If the variable isn't a string, you'd be getting a compile error)

How exactly are your controller variables (nextQuestionId, filter, questionBankId) being set?

If you're taking this as input from the first Visualforce page, you'd need to put that input in between <apex:form></apex:form>, and then submit the form to pass the values to your controller (which would then construct and return the PageReference) instead of using an <apex:commandLink>.

  • Thanks for the response, it was the setRedirect(true) method I was missing. Once I added this, the URL populates with all the parameters. – John_Henry Sep 29 '15 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.