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
theitdeptrockstheitdeptrocks 

QuickSave and then redirect (if no validation errors)

Hi all,


I'm working on a VF page with a few field validation checks.  What I'm looking to do is have a user complete a form, then click on the "Next" button to go to page 2.  Before going to page 2 I would like the perform a quicksave and then redirect to page 2.  If upon quicksaving any errors are found, I would like the error to be displayed and the user not redirected.

 

The code I have now tries to quicksave and regardless of its success, forwards the user to page 2.

 

 

<apex:pagemessages ></apex:pagemessages> ... <apex:actionFunction name="saveit" action="{!quickSave}" oncomplete="window.location='http://www.google.com';"/>

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
AvromAvrom

Sorry, my last message was a bit garbled and a bit misinformed.

 

You *can* return a page reference to an external page, and of course you only need to return null to stay on the same page. So here's your component code:

 

<apex:pageMessages id="error"/>

<apex:actionFunction name="SaveIt" action="{!doSave}" rerender="error"/>

 

And here's the doSave() function in your controller extension (assuming the underlying standard controller is in a variable called ctr):

 

public PageReference doSave() {

    try {

        this.ctr.Save();

    }  catch(DmlException ex){

        //this catches the errors and ensures they register on the page

        ApexPages.addMessages(ex);

    }

 

 

    //if there's an error message, perform a refresh

    // if not, redirect to google.com

    if (ApexPages.hasMessages()) {

        return null;

    } else {

        return new PageReference('http://google.com');  

    }

}

 

Hope this helps,

Avrom

 

All Answers

AvromAvrom

Is page 2 a Salesforce page? (It's not, in the example you give, but I don't know if it's supposed to be.) If so, instead of calling quickSave directly, you can write a controller with a method that calls the standard controller's save() and returns either the current page reference (if save() does) or a PageReference to the page you want to redirect to (if save() returns any other page).

 

If you really do want to redirect to an external page...well, that's a little tricky. Probably the easiest thing to do is to make a dummy VF page that redirects (to the site of your choosing) on load, and have your controller return a page reference to *that*.

 

Hope this helps,

Avrom

bob_buzzardbob_buzzard

The redirection will happen in all cases because there is an oncomplete attribute which changes the location of the page.  Oncomplete fires when the Ajax request is complete, regardless of what happened in the quickSave method. 

 

You'd need to have something in the oncomplete JavaScript to determine if any errors occurred in the method - check a controller success/fail property derived from the message count on the current page for example.  Then you could conditionally redirect based on the value of that property.

AvromAvrom

Sorry, my last message was a bit garbled and a bit misinformed.

 

You *can* return a page reference to an external page, and of course you only need to return null to stay on the same page. So here's your component code:

 

<apex:pageMessages id="error"/>

<apex:actionFunction name="SaveIt" action="{!doSave}" rerender="error"/>

 

And here's the doSave() function in your controller extension (assuming the underlying standard controller is in a variable called ctr):

 

public PageReference doSave() {

    try {

        this.ctr.Save();

    }  catch(DmlException ex){

        //this catches the errors and ensures they register on the page

        ApexPages.addMessages(ex);

    }

 

 

    //if there's an error message, perform a refresh

    // if not, redirect to google.com

    if (ApexPages.hasMessages()) {

        return null;

    } else {

        return new PageReference('http://google.com');  

    }

}

 

Hope this helps,

Avrom

 

This was selected as the best answer
theitdeptrockstheitdeptrocks
Thank you Avrom, that does exactly what I was looking for.  That was a huge help!
theitdeptrockstheitdeptrocks

So your response has been working great.  Now we are looking to do another questionnaire, but one that spreads across multiple pages.

 

I'd like to use this code again so validation rules are checked, the record is saved, and then the user is taken to the next page.  The question I have now is how do I make it so after the first page is saved (and the record is created) when they complete the 2nd page, a 2nd record isn't created.  The object created on a successful save on page 1 should be updated.

 

Any ideas?  I imagine it would require grabbing the ID of the created record, appending it as the controller's ID in the URL of the next page.  I'm just not sure how to go about grabbing that newly created record ID.

 

Any help is greatly appreciated!  Thanks!