I have done some looking around and I cannot seem to find a current solution to my issue.

I would like to call an apex method to remove records when a user leaves a visualforce page.

I've gone down the path of an action function, and am currently in the middle of JS Remoting. I can get the javascript method to successfully call my controller method when I call it directly from my command button, but I cannot get it to run on the unload event, even when I can confirm that I entered that method via the console logs.

Page Script:

    <script>

    $(window).unload(function () {
         console.log('I ran'); 
         removeUserFromPublicGroup();
    });


    function removeUserFromPublicGroup() {
        console.log('In method');
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.PolicyholderChangeRequestEXT.removeUserFromPublicGroup}',
            function(result, event) {
                if(event.status) {
                    console.log('Pass');   
                }
                else {
                    console.log('Fail');   
                }
            }
        );
    }

    </script>

Controller method

@RemoteAction
public static void removeUserFromPublicGroup()
{
    system.debug('Remove Server Method called');
}

I tried the beforeunload event and in various ways and I cannot register that that event has even been fired via console messages or debugger statements. And where unload does run and I am in the removeUserFromPublicGroup method, the controller method never runs as confirmed by my debug logs from the Developer console.

I have referenced this stackexchange similar question however it does not seem to work for me at all. As in, I don't get a console statement or hit a debugger.

I have also referenced this page for events to use when leaving the page.

Just to reiterate calling it from a command button DOES work and WILL execute my controller method via JS Remoting. But I need to run the same method when a user leaves the page. Which is NOT working even though the remove method was entered.

<apex:commandButton styleClass="slds-button slds-button_neutral" id="cancelButton" value="Cancel" onclick="removeUserFromPublicGroup();  cancel('{!holder.Id}'); return false;"/>
up vote 3 down vote accepted

Visualforce.remoting.Manager.invokeAction is almost certainly asynchronous in nature. I'm assuming by the time the unload function is completed the browser is about ready to kill everything on the page, including any outstanding async operations. So it never runs as the page is gone before it even gets a chance. See also - Doing an ajax call on window.unload

There is probably a more accurate way to describe that in terms of JavaScript event loops, the execution stack, and queued requests.

You will either need to switch to a synchronous operation or an entirely different mechanism to achieve what you want.

I'd probably go with the latter if you can. It's always going to be hit and miss trying to perform any action as the user transitions away from a page. A drop in internet connectivity, power loss, or any number of other scenarios could prevent your last second operation from occurring.

Instead, you could have a scheduled batch job find the records that removeUserFromPublicGroup() operates on. It could then do the required work regardless of the state of the users browser. If you are concerned about records being removed for active users, have something on the page that keeps updating a "last modified" field on the sObjects in question. The batch job could then leave the records for a period of time.

  • 1
    That's what I was afraid of. I wasn't sure if since the ajax call was invoked that it would run after the page was unloaded, which clearly it doesn't based on the behavior. This is an edge case scenario and I think the batch job to do any 'leftover' tasks is going to be what I have to do. Thank you – JRiffe Nov 8 at 21:31

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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