+ Start a Discussion
turbo2ohturbo2oh 

pagereference redirect not re-running constructor

I have a force.com external site page. My idea is after the user is done the task I want them to have the ability to return to the beginning and do it again. To do this I'm trying to refresh the current page and re-run the constructor so they'll see the new information they had just added.

 

I have following button:

<apex:commandButton styleClass="btn  btn-large" action="{!cancelThis}"	value="Add/Edit More Sites" />

 That launches this method that should be returning to the same page and refreshing it after re-inserting the URL paremeters:

 

public PageReference cancelThis() {
		PageReference currPage1 =  new PageReference('http://MYSITE.COM/MYVFPAGE');
		currPage1.getParameters().put('accountId',urlAccountId);
		currPage1.getParameters().put('contactId',urlContactId);
		currPage1.setRedirect(true);
		return currPage1;
	}

 

The issue I'm running into is its not re-running the constructor. I have a debug statement at the top of my constructor that doesn't show up when cancelThis() runs. If I manually refresh the page after the 'redirect' it does.

 

Any idea why the constructor isn't re-running when I'm setting redirect=true? Is it because it knows its going back to itself instead of a new page?

 

Thanks!

Daniel.ReidDaniel.Reid

I feel your pain.  I've had a very similar situation on one of my systems.  I do believe that pages will not re-initialize the controller unless forced to reload, or opened from another controller.

 

The "fix" we eventually went with was to pull the important initialization code out of the constructor, and put them in separate functions.  The constructor calls those functions, as do whatever 'internal refresh' functions we fit to add. Hope that helps!

 

Daniel Reid
Contact us - We can help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x102

turbo2ohturbo2oh

Haha, well I'm glad someone does! I had considering pulling it out into other functions as you described but I ended up just forcing a reload:

 

<a onclick="window.location.reload()" class="btn btn-large btn-primary" href="#">Add/Edit More Sites</a>

 

IshwarIshwar
to refresh the curent page and re-run the constructor, this works for me:
On VF pafe create button
<apex:CommandButton value="Refresh" onclick="refreshPage(); return false;"/>
Then the JS function:

function refreshPage()
        {
            window.location.reload(true);
        }