I am currently having a VF page where when a customer pushes a button, a new custom object PRL__c is created for a certain contact and cardType. Before it is inserted, I perform a check:

  List<PRL__c> prlObject = [Select Id, ParentCase__r.Contact.Name from PRL__c where (Status__c != 'Cancelled' and Status__c != 'RS') and ParentCase__r.Contact.Id = :contactId and Type__c =:typeVariable];
  if(prlObject.size() > 0) {
        //Throw error

So if the Contact has already an PRL__c with the same Type__c, the new PRL__c does not get inserted.

However, if I spam the button on the VF page, multiple PRL__c with the same Contact (linked over the case) and Type__c get inserted.

I suspect the following:

  1. The button is pushed for the first time
  2. The first check passes
  3. The button is pushed for the second time
  4. The second check passes
  5. The first item gets inserted
  6. The second item gets inserted

How can I prevent this?

Thanks a lot!


This is a common scenario that many of the web-applications come across.The ideal way to handle such cases would be either to disable whole page or button itself as soon as button is clicked for first time. You can use <apex:actionstatus> tag in combination with <apex:commandbutton>to achieve same. Button will be disabled and will show 'saving...' as label as soon as it is clicked for the first time.Button will be activated back as soon as your operation completes. It will not allow user to hit button once again and cause problems that you are facing.

Here is an example to understand sample implementation for your reference.

  • Thank you for your response! I have a hard time with Client-Side Solutions because that implies that not two different users perform the same action on different devices. Am I right? Are there any server-side alternatives?
    – fat32
    Sep 6 '16 at 10:19
  • In case you need a server side solution, you may try using a FOR UPDATE in end of your SOQL. Although you are not updating, but it will attain a lock on record and will prevent next transaction to access same. It will throw Query exception for next transaction. Sep 6 '16 at 10:28
  • We once tested concurrency from different devices and tried hitting a button all together. Not sure about the reason but we were not able to fail our concurrency test case. Sep 6 '16 at 10:33

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.