You need to sign in to do that
Don't have an account?
Brian Cherry
Explicit ORDER BY not allowed when locking rows
I'm trying to get pull the oldest case and assign it to a user. Without locking, we are experiening where users are clicking the button to call the query at the same time. If I add in For Update and remove order by, the oldest case isn't ever pulled. The newest case is. Does anyone have a work around to query the oldest case for update?
Case caseObj = [select c.CreatedDate,c.ID,c.OwnerId from Case c where
c.IsClosed=false
and c.OwnerId in :listGroupIds ORDER BY c.CreatedDate
limit 1
for update];
caseObj.OwnerId = userId;
update caseObj;
return caseObj.Id;
}
catch (QueryException e) {
System.debug ('No Cases Available');
}
Case caseObj = [select c.CreatedDate,c.ID,c.OwnerId from Case c where
c.IsClosed=false
and c.OwnerId in :listGroupIds ORDER BY c.CreatedDate
limit 1
for update];
caseObj.OwnerId = userId;
update caseObj;
return caseObj.Id;
}
catch (QueryException e) {
System.debug ('No Cases Available');
}
All Answers
Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
NOTE:-
1) While the records are locked by a client, the locking client can modify their field values in the database in the same transaction. Other clients have to wait until the transaction completes and the records are no longer locked before being able to update the same records. Other clients can still query the same records while they’re locked.
2) If you attempt to lock a record currently locked by another client, you will get a QueryException. Similarly, if you attempt to update a record currently locked by another client, you will get a DmlException.
3) If a client attempts to modify a locked record, the update operation might succeed if the lock gets released within a short amount of time after the update call was made. In this case, it is possible that the updates will
You can’t use the ORDER BY keywords in any SOQL query that uses locking
http://amitsalesforce.blogspot.in/2015/03/locking-statements-for-update.html
https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_locking_statements.htm
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks,
Amit Chaudhary
amit.salesforce21@gmail.com
Thanks for taking the time to respond. I understand that I can't sort records when doing a a for update. However, I was seeing if someone has a work around for this. If it's possible to lock the record after the query, have it assign the case, then remove the lock to avoid the case being assigned twice.The problem with Salesforce for update queries, is it pulls the newest records, I need the oldest records.
Thanks,
Brian.
I have a similar issue and i could not get it to work. It throws an error : Compile Error: Initial term of field expression must be a concrete SObject: List<Lead> at line 27 column 77
Here is the code :
Any help would be appreciated. Thanks