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
jucuzoglujucuzoglu 

How do I redirect the user after an Update?

I have created a VisualForce page which allows an object record to be updated.

 

I utilized site to display the page, the user can go in edit fields and Save which updates the page.

 

The problem is that after the user updates the page it is defaulting to mysite.force.com/siteName/SalesforceID

 

When an update occurs I would like to send my user to a thankyou page. How do I intercept the URL it is going to send the user to and redirect that user instead somewhere else?

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

I'm assuming that you have an Apex controller/extension tied to your VF page that has a 'save' method that gets invoked when the user hits 'Save' on the page. If so, you can do something like this in the 'save' method:

 

public PageReference save()

{

//logic to save your data

PageReference p = Page.ThankYou;

       //This assumes that your thank  you page is also a custom VF page. If not, then you can do something like PageReference p = new PageReference('your Thank you page URL');

       p.setRedirect(true);

return p;

}

 

 

All Answers

forecast_is_cloudyforecast_is_cloudy

I'm assuming that you have an Apex controller/extension tied to your VF page that has a 'save' method that gets invoked when the user hits 'Save' on the page. If so, you can do something like this in the 'save' method:

 

public PageReference save()

{

//logic to save your data

PageReference p = Page.ThankYou;

       //This assumes that your thank  you page is also a custom VF page. If not, then you can do something like PageReference p = new PageReference('your Thank you page URL');

       p.setRedirect(true);

return p;

}

 

 

This was selected as the best answer
jucuzoglujucuzoglu

I am using the standard controller. Is there an example you could provide or point me to that more or less extends the controller in a manner that would allow me to employ your code solution.

forecast_is_cloudyforecast_is_cloudy

You will need to create a controller extension class, associate it with you VF page and then override the standard 'save' method in the controller extension with the code that I had posted earlier. More details on controller extensions can be found here -  http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm.

 

Hope this helps....

jucuzoglujucuzoglu

Here is what I have, the code is running (because I see the field updating) but the redirect does not seem to be happening. Any idea what I might be doing wrong?

 

 

  public pageReference saveAndRedirect()
  {
      Contact_Update__c cu = new Contact_Update__c();
      // Set the value of the Status field to OPEN
    cu.Status__c = 'Open';
    
      controller.save();
      PageReference submissionPage = new PageReference('http://www.google.com');
    submissionPage.setRedirect(true);
      return submissionPage;
  }

 

 

forecast_is_cloudyforecast_is_cloudy

Is the commandButton on the VF page tied to the 'saveAndRedirect' method (and not 'save')? It should look something like:

<apex:commandButton action="{!saveAndRedirect}" value="Save"/>

 

 

 

 

jucuzoglujucuzoglu

Its very similar but there is a difference. Do you think it is causing the issue?

 

 

<apex:commandButton action="{!saveAndRedirect}" value="Update" /> 

 

 

forecast_is_cloudyforecast_is_cloudy

No - that looks correct. What happens after the user hits 'Update'? Are they still being redirected to the record detail page? Or do they remain on the existing VF page?

Also, can you check in your Debug Logs to make absolutely sure that the 'www.google.com PageReference code is being invoked? Perhaps you can put a couple of System.debug statements to be absolutely sure.

jucuzoglujucuzoglu

They end up at the object page so like s6.salesforce.com/(objectID)

jucuzoglujucuzoglu

I figured out why the redirect was not working, something was erroring out in my code. I think my issue is one of scope.

 

See the page below

 

 

public class ext_ContactUpdate {

  private ApexPages.StandardController controller;

  public ext_ContactUpdate(ApexPages.StandardController controller) {
    // Grab the Contat Update Record
    Contact_Update__c cu = (Contact_Update__c)controller.getRecord();
  }
  
	public PageReference save()
	
	{	
		
		//logic to save your data
		PageReference p = new PageReference('http://www.google.com');
		p.setRedirect(true);
		return p;
	
	}

}

 

I need to figure out how to access the cu object fields in the Save method and the proper syntax to perform the Save logic. The code I was using before was breaking which is why it appeared to not be re-directing.