I'm not sure if anyone is able to answer this question (I've been to Salesforce Support already who weren't any help on this issue) but I thought I'd ask it anyway and see if anyone has any ideas how to solve.

Basically, we have various visualforce pages that will run one off code that creates lots of child records for a certain parent record. These records will run their own triggers and so can take quite a while to complete.

My issue is if a user goes to the visualforce page to run the code while it is already being run by another user, at the end of both transactions there will be two sets of child records that have been created. Is there a way to detect when a user is already running code so that I can throw errors if someone is already processing?

I've already tried adding 'for update' keyword onto my SOQL statements but what this does is to pause the second transaction until the first is completed (so the second transaction will eventually execute, which I don't want)

((My thoughts would be to try something along the lines of partial processing - so I would update a record to flag that a transaction is in progress, commit that to the database then run the actual logic afterwards? Either through javascript or @future methods.))

1 Answer 1


The way that I've handled this in the past is to use a custom setting exactly as you have described. When the user presses the button on the visualforce page to start the processing, the first thing that happens is the custom setting is checked to ensure processing isn't ongoing and then updated to indicate the processing has started. I then proceed in a couple of different ways depending on the requirements:

  1. A flag is then set in the controller to get the page to send the start processing request (via an actionfunction and some javascript that is only rendered if the flag is set).
  2. The processing is kicked off asynchronously via batch apex or an @future method

If other users are already on the other visualforce pages and press the button to start the processing, the custom setting check fails and they get an error/warning that processing is already happening.

For users that navigate to the visualforce pages anew, I incorporate the custom setting check into the rendering code and disable the button to start processing if it is in progress. I also have an indicator on the page that shows the processing status at the top right - you can be a bit more clever with this and tie it to an actionpoller that periodically updates the status and shows progress.

As the final part of the processing, the custom setting is cleared.

  • I would add that if this process is specific to certain records a custom field to track the state on that object might give a better granular approach.
    – Mike Chale
    Commented Jan 7, 2013 at 12:24
  • Very interesting. I will need to try this approach when I get around to it. I'm still shocked that something like this isn't available out of the box.
    – Ben
    Commented Jan 7, 2013 at 13:16

You must log in to answer this question.

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