[SalesForce] Lock and Unlock opportunity object during approval process

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?

Best Answer

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!