Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.))

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
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 Jan 7 '13 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 Jan 7 '13 at 13:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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