function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike ChandlerMike Chandler 

How do you conduct a page redirect after an insert/update?

I'm working on an Apex custom controller for a Visualforce page.  After I perform an insert transaction for a new instance of a custom object, I want to redirect to a Contact detail page.

I have a field on the Apex class called myContact of type Contact and my constructor always makes sure it is valid and instantiated with a valid Contact based on URL parameters passed to the Visualforce page, so I always have a Contact.  After I do my new custom object insert, I invoke a method called returnToContact() which is shown below.  However, it doesn't redirect, leading me to believe I'm doing something wrong.
public PageReference returnToContact() {
        PageReference contactPage = new PageReference('/' + myContact.Id);
        contactPage.setRedirect(true);
        return contactPage;
}
Any ideas on what I need to do to ensure I can properly redirect?
Best Answer chosen by Mike Chandler
John GerhardJohn Gerhard
Mike,

This code will redirect the current page to contact you are passing into it. I would say if this is not working then the method is not being invoked.

All Answers

John GerhardJohn Gerhard
Mike,

This code will redirect the current page to contact you are passing into it. I would say if this is not working then the method is not being invoked.
This was selected as the best answer
Mike ChandlerMike Chandler
Hi John,

I think that's a reasonable assessment.  However, I went ahead and included a System.debug('Redirecting!') message in the method.  That debug log appears in the console, yet the redirect doesn't take place.  Could there be anything else that I'm overlooking?
Mike ChandlerMike Chandler
Actually... it's working now!

My VisualForce page was submitting a form to an action method with a void return type.  That action method was invoking the page redirect method.  The method was being invoked, but the PageReference wasn't being returned by the original method.  By refactoring the action method to return the PageReference that's returned by the returnToContact() method, I got my redirect working.

Thanks John!  I would have stumbled around a little longer had you not pointed me in the right direction! :)
John GerhardJohn Gerhard
Mike,

As a peace of mind I applied the same logic to the account object and tested it out very quickly myself. However, I called the method via a command button. It works as intended, even if the account.Id is NULL then it would redirect you to your home page.

User-added image
Mike ChandlerMike Chandler
Yeah, I think it would have been a bit straight forward had my process involved calling that method directly from the command button.  The redirect was an afterthought for me, so it managed to keep me mystified for longer than I care to admit! ;)  Thanks again for the help John!

Mike
Mike ChandlerMike Chandler
Actually, here's what I was doing:
public void save() {
        // do lots of cool stuff here!
        returnToContact();
}

public PageReference returnToContact() {
        PageReference contactPage = new PageReference('/' + myContact.Id);
        return contactPage;
}
And here's what I needed to do since the VF page was calling save():
public PageReference save() {
        // do lots of cool stuff here!
        return returnToContact();
}

public PageReference returnToContact() {
        PageReference contactPage = new PageReference('/' + myContact.Id);
        return contactPage;
}