8

I have custom button which when clicked redirects to VF page, VF page calls some controller,

But I want as soon as custom button is clicked it should process to VF page waits for 2 secs and then redirect back to detail page of the record.

How to achieve that? Not getting any help from google

7

This is the most simple way I could imagine. Other alternatives abound, as well.

<apex:page controller="Controller" action="{!action}">
    <script>
        window.setTimeout(function() {
            window.history.go(-1);
        }, 2000); // 2 seconds
    </script>
    We did as you asked. Redirecting back in 2 seconds.
</apex:page>

As a side note, I'd avoid DML this way, because it does open up the possibility of XSS attacks (however unlikely that would be). Instead, offer the button here as a means of confirmation, then return when you're done. Alternatively, simply perform the work from the button itself. There's really no need to show a page for just two seconds.


You can also show a waiting dialog while you're waiting:

<apex:page controller="Controller">
Please wait...
<script>
function done() {
    window.history.go(-1); // Or elsewhere
}
</script>
<apex:form>
<apex:actionFunction name="doAction" action="{!doAction}" oncomplete="done()" />
</apex:form>
<script>
doAction();
</script>
</apex:page>

Feel free to use a spinner or something instead of just a generic "please wait" message to give the illusion that something is happening (which, something is happening, but we want the user to be reassured).

  • fox, the idea behind showing page to the user is it show them there record is processing and if it successfully updated boom ..the detail page is shown... – Ashish May 1 '15 at 1:32
  • @ashish In that case, you'd probably want to move this to an action function. It doesn't need to be 2 seconds, just as long as it takes... I'll update with example. – sfdcfox May 1 '15 at 1:41
  • True, Action sounds good , I haven't used it ever so was not aware of it .. But thanks for giving me so many options .. that was really awesome... I definitely owe you beer for sure... :) – Ashish May 1 '15 at 1:51
  • Another option would be to call a method in the controller that will return a page reference and do the redirect on the server side. – Boris Bachovski May 1 '15 at 3:52
  • Nice answer @sfdcfox! One could seemingly easily modify the method from this blog post for a progress bar inside of a controller as @Bachovski suggests to give the visual impression something was happening and control the timing of it. – crmprogdev May 1 '15 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.