function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tiia Tirkkonen 18Tiia Tirkkonen 18 

How to unlock a record that is in an approval process?

I have a record of the Incident object that is automatically submitted for approval upon creation. The approval process locks the record, but I would like to unlock it right away.

Now in my trigger I have:

trigger IncidentBeforeTrg on Incident__c (before insert, before update) {
for(Incident__c newInc:trigger.New){
if(Trigger.isUpdate && Approval.isLocked(newInc.Id)){
Approval.unlock(newInc.Id);
}
}
}

But this does not seem to be doing anything. What am I missing?
RD@SFRD@SF
Hi Tiia,

If you dont want to lock the records,

Why dont you set the initial submit actions to unclock the record, or do you have a criteria for unlocking them

Hope it helps
Tiia Tirkkonen 18Tiia Tirkkonen 18
I can't seem to find the option to unlock the record as an initial submission action. Where can I do this?
pkpnairpkpnair

you can unlock it using apex since winter 16.. 

But To enable this feature, from Setup, enter Process Automation Settings in the Quick Find box, then click Process Automation Settings. Then, select Enable record locking and unlocking in Apex. Have you done that?

RD@SFRD@SF
Hi Tiia,

What is approval process for?
You can unlock the record in the final approval or reject actions

Regards
RD
 
Tiia Tirkkonen 18Tiia Tirkkonen 18
pkpnair: I have the setting enabled

RD@SF: I need to unlock the record when it enters the approval process, so it can be edited by anyone also while in the process. Unlocking it at approval or rejection is not sufficient.
pkpnairpkpnair
Are you sure this is a Before Update Tirgger? Have you tried after update?
RD@SFRD@SF
Tiia,

Why would you allow the approving record to be allowed to be edited. In case you have multiple approving steps

Step 1 needs a value say 'A' to be true.

Step 2 has another  criteria.

if you allow the record to be edited.In between the steps the value A might be edited to false.

Which fails the concept.

Hope you get the idea.
I am just trying to figure out what you want to achieve.

RD
pkpnairpkpnair
did you change the trigger to after trigger?
pkpnairpkpnair
How did you solve this?
Tiia Tirkkonen 18Tiia Tirkkonen 18
I moved it to the after trigger and removed Trigger.isUpdate, now it appears to be working.
pkpnairpkpnair
Could you please mark the answer if it helped you to solve it?
dellseysdellseys
@Tiia Tirkkonen and @pkpnair
what will be an apex test class for the code below

@Tiia this is your code below, I just modifed it basd on your explanation. If the below is right.. what will be the code coverage (apex test class to use)

trigger IncidentBeforeTrg on Incident__c (after update, after insert ) {
for(Incident__c newInc:trigger.New){
if(Approval.isLocked(newInc.Id)){
Approval.unlock(newInc.Id);
}
}
}
Sanjana BheemreddySanjana Bheemreddy
This trigger is working fiee for system admin profile but not for any custom profiles. 

Can you please help. 

Thanks,
sanjana
Ryan Bailey 14Ryan Bailey 14

All,

I just got done solving this issue the other day. Here is what you need to do!

1.) in all steps of approval process update a field to a value. In my case I was updating a status. In all these status updates set 're-evaluate workflow rules' 


2.) Create new workflow rule to fire on 'true' anytime record is created or updated.  

3.) Create field update to update a date time field with now(). This will cause force fire of trigger on object. 

4.)write trigger onAfterUpdate to unlock record. 

5.) please note once a record is locked you cannot perform dml statements on an object as checking for record lock occurs during system validations in order of the execution which happens before trigger execute. 


need more info just ask as this is working for me. 

Eric PerowneEric Perowne
Hey Ryan,

I am working on a similar issue, though am not equiped with the same competencies and so require further guidance.

If I understand correctly, the workaround is to create a workflow rule to update a time field with now(), which is then leveraged by a trigger.

As I have yet to learn Apex, am I out of luck for the time being, or is it possible to copy/paste some modular code and adapt it to my needs?

---

My use case is: A custom object used by multiple departments must enter an approval process for a subsection of stakeholders. While approving, the object must remain unlocked save for specific fields (which I can achieve with Object Manager -> Data Validations & the Status field you mentioned at the beginning).

Thanks!
Nikhileshwar Ikkurthi 2Nikhileshwar Ikkurthi 2
*have a custom field updated from the approval process and let it fire the trigger 
*In the trigger check for the condition of the updated field and call a future method with the unlock record code.
*this ensures two transactions and it works!
 
public with sharing class UnlockRecord {   
  
 @future     public static void Unlock(List<Id> requests) {   
      for(Id accountId :requests){                         
       try{                 
              Approval.UnlockResult unlockResult = Approval.unlock(accountId);         
            }            
 catch(exception ex){                 
           system.debug('Exception*****'+ex);            
     }                       
   }        
 } 
}