3

I'm trying to implement locking in sake of not having duplicates. The problem is it works only with this line System.debug(LoggingLevel.DEBUG, 'duplicateCallLog.size() == 0 - '+ duplicateCallLog.size());. It sounds like System.debug call guarantees that result of insert will be available in other instances of controllers. Is there any procedure in APEX aka commit or saveChanges that grants data in database? Or there any other locking patterns in APEX?

   List<CallLog__c> duplicateCallLog = [SELECT Id FROM CallLog__c WHERE SessionID__c = :sessionId ORDER BY CreatedDate DESC NULLS LAST LIMIT 1];
        if(duplicateCallLog.size() == 0)
        {
           System.debug(LoggingLevel.DEBUG, 'duplicateCallLog.size() == 0 - '+ duplicateCallLog.size());
           insert newCallLog;
           newCallLog = [SELECT Id FROM CallLog__c WHERE SessionID__c = :sessionId ORDER BY CreatedDate DESC NULLS LAST LIMIT 1];
           callLogId = newCallLog.Id; 
        }
        else{
           System.debug(LoggingLevel.DEBUG, 'duplicateCallLog.size() != 0 - '+ duplicateCallLog.size());            
           callLogId = null;     
        }                       
1
  • 1
    About locking patterns -- you can read advancedapex.com. I would recommend you to do your fix on trigger level.
    – kurunve
    Jan 27, 2016 at 16:26

1 Answer 1

1

I believe what you are looking for is the "FOR UPDATE" keyword on your SOQL calls.

A great resource is Locking Statements

The idea is that you can do the following:

List<Contact> contact = [SELECT Id, Name, Phone FROM Contact WHERE Id IN :idSet FOR UPDATE]

This will allow you to lock the records while you are editing and ensure that no other processes modify the records you pull from the database.

2
  • I need something that will lock whole table
    – AndrewG
    Jan 27, 2016 at 22:11
  • I'd suggest reading the linked article as it goes over what your asking about. Jan 27, 2016 at 22:15

You must log in to answer this question.

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