3

The Visualforce page has

<apex:commandButton action="{!submit}" value="Submit"/>

The submit method returns void.

public void submit()

Now the page has certain URL parameters, the submit method correctly possesses them and works as expected except that while returning, it deletes URL parameters.

The URL '/apex/pageName?var1=abcd&var2=pqrs' becomes '/apex/pageName'. The parameters are gone. Problem with this is when user refreshes the page, it doesn't work as expected as url parameters are not there.

When I change submit method to return PageReference of current page, it rewrites URL with URL parameters + view state which looks unclean.

        PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
        pageRef.setRedirect(true);
        return pageRef;  

How can we ensure that URL parameters are not changed.

2 Answers 2

4

If you are not redirecting the page then you should use rerender attribute.

<apex:commandButton action="{!submit}" value="Submit" rerender ="form"/>

Just give outer form the Id and then rerender it instead of refreshing the complete page,. It will not remove URL parameters.

1

From Salesforce Docs:

When a redirect occurs the controller clears the context state. Consequently we need to reset the query string parameter in the PageReference's parameter map.

Thus your code will be.

PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
pageRef.setRedirect(true);
pageRef.getParameters().put('var1', ApexPages.currentPage().getParameters().get('var1'));
pageRef.getParameters().put('var2', ApexPages.currentPage().getParameters().get('var2'));
return pageRef;  

Src: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_navigation_methods.htm

3
  • 1
    The parameters are still there, just hidden in a form post. They're not actually redirecting anywhere, that's just how VF works.
    – sfdcfox
    Dec 19, 2018 at 11:35
  • But when the user refreshes by clicking browser button, they would be lost unless theya re in URL params? Dec 19, 2018 at 11:38
  • 2
    Maybe, it depends on how the user refreshes, honestly. Either way, it isn't as simple as writing Apex code to fix in this case.
    – sfdcfox
    Dec 19, 2018 at 11:43

You must log in to answer this question.

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