1

I have a visualforce page that runs calculations and stores them in an sObject as "temporary" until a user clicks a Save button, which removes the temporary flag. This was done because the calculations take longer than the max CPU time limit available, and a batch process does the calculations while the page polls for results to be completed.

My issue is that when a user doesn't save the records, and navigates away from the page (clicks any link, closes the window, etc), I want to delete the temporary records.

I've tried doing JS Remoting like this, but it didn't work:

window.onbeforeunload = function() {
    // delete the temporary records.
    Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.MyController.deleteTemporaryRecords}','{!reportId}',handleResult);
    console.log('deleting temporary records. {!reportId}');
}

I've also tried the different syntax for JS Remoting like this:

window.onbeforeunload = function() {
    // delete the temporary records.
    MyController.deleteTemporaryRecords('{!reportId}',handleResult);
    console.log('deleting temporary records. {!reportId}');
}

I've also tried using an actionFunction inside the apex:Form

<apex:form>
    <apex:actionFunction name="deleteTemporaryRecords" action="{!deleteTemporaryRecords}"/>
</apex:form>

window.onbeforeunload = function(){
    deleteTemporaryRecords();
};

I also tried calling a @future method from my controller method so that the delete happens asynchronously, but that hasn't worked either.

Has anyone been able to successfully run a controller method during window.onbeforeunload or window.onunload events?

1

Per this StackOverflow post, I suggest you update your onbeforeunload to return null; like so:

window.onbeforeunload = function() {
    // delete the temporary records.
    MyController.deleteTemporaryRecords('{!reportId}',handleResult);
    console.log('deleting temporary records. {!reportId}');
    return null;
}

That said, I would be very wary of this approach. Clients will not necessarily fire this event, so you shouldn't depend on it to delete the temporary records. Instead, I would probably run a regular scheduled job to delete temporary records that were created > x minutes or hours ago.

  • 1
    Wow, simple oversight on my part for returning null. Thanks :D. As for the batch, I didn't want a user's temporary records to get deleted while they might still have the page open. I thought about doing a scheduled batch to run every 15 minutes or so, that would delete all temp records with LastModifiedDate > some configurable number of minutes in the past. I may add a timeout function to the page that would delete the records after a certain time interval, then refresh the page. That way, any kind of navigation from the page, or no activity at all would both fire the the same method. – Jason Benkert Jul 8 '16 at 13:02

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.