As per Salesforce documents, I understand that when using SELECT FOR UPDATE the records will not released until the end of transaction.
I use SELECT FOR UPDATE but still find error UNABLE_TO_LOCK_ROW when I call UPDATE after it. Is it possible to get the error if the records still being locked?
I doubt that my records were released before transaction end. Perhaps because the SELECT FOR UPDATE is in separated method? Will the locked rows release after method exit?
My code is like
Public static void updateSalesOrder(Order so){
//prepare value for update so here
Order record;
try{
record = LockRecord(so.id); // call method to lock so by id
} catch(QueryException e) {
//log error
}
If(record!=null){
update so;
}
}
Public static Order LockRecord(Id soId){
return [SELECT Id FROM Order WHERE Id =: soId FOR UPDATE];
}
Order
may not be the record that is subject to the row lock exception.