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
Somasundaram S 1Somasundaram S 1 

How to unlock the record

Hi All
      how to unlock the record through code or through process builder , could you please advise me
thanks
Best Answer chosen by Somasundaram S 1
karthikeyan perumalkarthikeyan perumal
Hello 

Using Code: 

if you have your code written directly in the trigger or in a class designated without sharing, then the apex code can update records that are locked, regardless of the user who caused the trigger to fire. I tested by creating a trigger on Contact that updates a field on the Contact's Account. When the Account is locked, I can edit the contact record with a non admin user and the field on the Account gets updated.
If you use code in a class marked with sharing that is called by the trigger, then you will get an exception. If the trigger was executed by an Administrator, then the code will always run

For Example I have added to code unlock Opp
 
public class UnlockRecordDuringApprovalCon {
    Opportunity objOpportunity;
    // String objOpp;
    String currentuserId = UserInfo.getUserId();
    
    public UnlockRecordDuringApprovalCon(ApexPages.StandardController controller) {
        objOpportunity = (Opportunity)controller.getRecord();
    }
    
    public void processRecord() {
        Approval.UnlockResult unlockedRersult = Approval.unlock(objOpportunity);
        // Iterate through each returned result
        if (unlockedRersult.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : unlockedRersult.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('opportunity fields that affected this error: ' + err.getFields());
            }
        }
        System.debug('currentuserId ' + currentuserId);
        
    }   
}




Admin View: 
Setup->Create-> Workflows and Approvals -> Process Automation Settings

and set checkbox Enable record locking and unlocking in Apex as true
User-added image
Mark Best ANSWER if its Solved.

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello 

Using Code: 

if you have your code written directly in the trigger or in a class designated without sharing, then the apex code can update records that are locked, regardless of the user who caused the trigger to fire. I tested by creating a trigger on Contact that updates a field on the Contact's Account. When the Account is locked, I can edit the contact record with a non admin user and the field on the Account gets updated.
If you use code in a class marked with sharing that is called by the trigger, then you will get an exception. If the trigger was executed by an Administrator, then the code will always run

For Example I have added to code unlock Opp
 
public class UnlockRecordDuringApprovalCon {
    Opportunity objOpportunity;
    // String objOpp;
    String currentuserId = UserInfo.getUserId();
    
    public UnlockRecordDuringApprovalCon(ApexPages.StandardController controller) {
        objOpportunity = (Opportunity)controller.getRecord();
    }
    
    public void processRecord() {
        Approval.UnlockResult unlockedRersult = Approval.unlock(objOpportunity);
        // Iterate through each returned result
        if (unlockedRersult.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully unlocked opportunity with ID: ' + unlockedRersult.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : unlockedRersult.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('opportunity fields that affected this error: ' + err.getFields());
            }
        }
        System.debug('currentuserId ' + currentuserId);
        
    }   
}




Admin View: 
Setup->Create-> Workflows and Approvals -> Process Automation Settings

and set checkbox Enable record locking and unlocking in Apex as true
User-added image
Mark Best ANSWER if its Solved.

Thanks
karthik

 
This was selected as the best answer
Somasundaram S 1Somasundaram S 1
Karthi
            nice but is there any otherway to unlock the record by using simple process builder. 
karthikeyan perumalkarthikeyan perumal
Hello

we can use process builder for record lock.but we can't use process builder or flow to unlock the records. 
This is the idea about unlock the record using process builder and flow share in idea exchange. Kinldy have a look.

https://success.salesforce.com/ideaView?id=0873A000000PU1hQAG

Hope it's clear.mark solved if it's clear.

thanks
karthik
 
Somasundaram S 1Somasundaram S 1
Karthi 
            you are awesome . I will follow ur apex code and am new to development , it would be nice if i get test code for apex i will use trigger after update and call your class . 
karthikeyan perumalkarthikeyan perumal
Mark it best ANSWER if it's clear to provid the answers to others who is searching on the same topic 
Somasundaram S 1Somasundaram S 1
sure I will do it could you please send me test code for your apex class to me at sfsomu@gmail.com
karthikeyan perumalkarthikeyan perumal
Hello 
Test class with code coverage screenshot has been sent your mail id. please check 

Thanks
karthik
 
Mukesh Kumar 107Mukesh Kumar 107
Did this really work for you? What we find that a trigger calling Apex Unlock code can not unlock the record when the trigger is fired through Approval Process activities.
Harshita RastogiHarshita Rastogi
Hi,
Can you please send the test code for your apex class to me at hrustagi1991@gmail.com
Manglesh Shah 8Manglesh Shah 8
Could you please send the test code to me at manglesh.shah@shapegroup.com.au