Update records which also are updated by by batch process

after-triggerbatchlockingupdate

We currently have a batchprocess running from a managed package which runs every day for almost the whole day to update records of an object. When a user tries to do an update on this object we have a trigger which does an after update on this object we get the error:

first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 1 records:

As far as I have read this is caused by records being locked by the batch proces. One solution would be to add FOR UPDATE in the soql but since this is a managed package this is no option.

What would the solution be? Since I suppose that only the current batch gets locked, can this be resolved by passing the update from the trigger to a future class or should this be resolved in a different way?

Thanks in advance

UPDATE

I found a possible solution:

Approval.isLocked(recordId)

This method also accepts List, SObject, or List.

What I do not understand is how to use this.
The return value is a map with Ids and Boolean values if locked or not.

I.e.

Approval.isLocked(listToUpdate);

Do I loop trough the map until the list is unlocked and then?:

update listToUpdate;

UPDATE

  • The record where this fails has 5 childrecords
  • I tried with future but this throws an error since the batch is also future

Best Answer

Since the batchprocess running from the managed package was causing our trigger to fail. I ended up removing the updates from the trigger and adding to a batch proces running every half an hour which seemed to resolve the issue.

Related Topic