1

I have a Visualforce page that's a form which is linked to my custom object in Salesforce.

However, I want the form to be saved and then redirect the user to my second Visualforce page (Thank You Page) without using an apex extension page.

I was told I can use JavaScript to do so however the code that I have written is not working as expected.

<apex:actionFunction name="saverecordandredirect" action="{!save}" oncomplete="location.href = '{! $Page.ThankYouPage }'" />

1
  • 2
    Call this saverecordandredirect(); method from your JavaScript code. Commented Dec 18, 2017 at 10:29

1 Answer 1

1

One of the possible approaches certainly is redirecting with JS, but another is to do it in the actual backend. I think doing it in the backend with Apex tends to be much cleaner code-wise, but that's just an opinion.

Were you to want to do it with JS, then you're looking for something like this:

<apex:page standardController="Account">
    <apex:form>
        <apex:commandButton value="Submit" action="{!save}" oncomplete="redirect();"/>
    </apex:form>
    <script>
    function redirect() {
        window.location = '{!urlfor($Page.OtherVisualforce)}';
    }
    </script>
</apex:page>

But, were you on the other hand interested in trying the Apex approach, then you would need a method like this in the controller which you would call from the submit method:

public with sharing class YourController{

    public pageReference save(){
        /* Logic, DMLs, and other stuff here */
        Pagereference pr = New PageReference("http://www.google.com");
        pr.setRedirect(true);
        return pr;
    }
}
6
  • Hi Javier García Manzano, Thanks for the reply however, it didnt work. Also, I know how to do it with an apex extension however, I'm trying to do it without using an extension to redirect and save. Thanks
    – Ola
    Commented Dec 18, 2017 at 15:03
  • 1
    @Ola the first snippet does not work? Have you tried copy-pasting it in a new page and trying it? The JS redirection is supersimple. Commented Dec 18, 2017 at 15:45
  • @ Javier, No it doesnt work. & i just tried to copy paste in a new page and it didnt work. I also got an error stating "Unknown method 'Turndown__cStandardController.doSubmit()'". Thanks
    – Ola
    Commented Dec 19, 2017 at 9:01
  • @Ola that's because you're using the Turndown standard controller in your page. In my snippet there is no controller included and the "doSubmit" was just an example... If you want to use the standard controller use the standard method "save". I'll change it tho for the sake of it. Commented Dec 19, 2017 at 9:22
  • thanks its now redirects to the google page however, the form is not saving to the custom object in Salesforce which is just as important. Also, I need to redirect to another visualforce page rather a website page. So im guessing that part is easily changeable? Thanks
    – Ola
    Commented Dec 19, 2017 at 10:47

You must log in to answer this question.

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