1

I have an approval process for the Opportunity object. But I want to unlock a record during this process to add a very important information that can only be obtained later and then lock the record again. I've tried this code:

Opportunity op= [Select someCustomField FROM Opportunity WHERE ID = : param.OportunidadeId];

op.someCustomField = 'Very important information not initially provided.';

Approval.unlock(op);
update op;
Approval.lock(Oportunidade);

But I received the error below:

System.DmlException: Delete failed. First exception on row 0 with id xxxxxxxxxxxxxxx; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

Why do I get this error? How do I fix it?

4
  • 1
    Not sure this switch is still there but see Set Approval Process Locks and Unlocks with Apex Code for a possible "Enable record locking and unlocking in Apex" setting you could check.
    – Keith C
    May 10, 2018 at 21:01
  • 2
    Is that the exact error message? It specifically says "delete failed" which seems to be different than what your question is asking. Your sample code shows an update but this is a delete. Is it possible that there is a trigger or something trying to delete a different locked record?
    – gNerb
    May 10, 2018 at 21:32
  • @gNerb I also found it strange, but this is the error message that exists shortly after attempting to unlock the opportunity. There is no trigger or something similar that attempts to delete the record. Something I forgot to comment on is that when I try to run the code with an admin user the unlock works, but in other cases it does not.
    – CleberTXR
    May 11, 2018 at 11:21
  • I can confirm, that Approval.unlock(), does trigger that delete. Would be interesting to debug what is being deleted there.
    – Schnaps
    Mar 17, 2021 at 12:57

1 Answer 1

4

In your class declaration are you applying the sharing rules? If you try to disable them to ensure that the rules for the current user are not applied with the keyword "without". In this way:

public without sharing class nameOfYourClass{

    //Your code here
    Opportunity op= [Select someCustomField FROM Opportunity WHERE ID = : param.OportunidadeId];    

    op.someCustomField = 'Very important information not initially provided.';

    Approval.unlock(op);
    update op;
    Approval.lock(Oportunidade);
}

Regards!

0

You must log in to answer this question.

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