I need a mechanism to detect if a record is currently locked by an approval process and it would block the user running the Apex code from modifying the record.
First, some context for where this is a problem.
- There is a trigger on Opportunity that will create some master-detail related records when the Opportunity is modified.
- The trigger is part of a managed package that is installed in a subscriber org
- The subscriber org is using an approval process on the Opportunity that has locked the record.
- The subscriber org has their own master-detail custom object related to the Opportunity.
- When they modify their related object on the locked Opportunity it causes all the Opportunity triggers to fire (because they have a custom rollup summary field on Opportunity referencing their custom object).
- The managed Opportunity trigger is failing with the message:
ENTITY_IS_LOCKED, This record is locked. If you need to edit it, contact your admin.: 
So, I clearly don't want to try to create my managed related object when the user doing the transaction can't modify the locked record.
But how can I tell if the current user can modify the Opportunity or not?
I can use
Approval.isLocked(id) from Winter '16 to check if the record is locked by an approval process, but it doesn't tell me if the current user can still modify it. I checked for a System Admin user who could modify the record and it still returns true.
I guess I could just try the DML operation and handle the exception, but that sounds expensive.
Another option might be to try
without sharing on the trigger and just outright ignore the approval lock - Are triggers able to modify records locked by an approval process?