+ Start a Discussion
Jack VolkovJack Volkov 

Does unlock() method work for records locked by time-based workflows too?

Will unlock() work for records locked by workflow rules as well, or only for records locked by an approval process?

https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_approval_locks_unlocks.htm
karthikeyan perumalkarthikeyan perumal
Hello Jack, 

Create a Record Type called Locked_Readonly  and the make all field are readonly with Unlock button.
Try to update record type Id suing workflow rule. add the time trigger to change the record type time bsed. 

for Unlock record try to assign base record type id throug code. 

hope it may helps you.
Thanks
Karthik



 
Jack VolkovJack Volkov
Still does not answer the original question, does unlock() apex method work for records locked by workflow rules? 
karthikeyan perumalkarthikeyan perumal
Hello Jack 

Yes its possbile to lock /unlock the records using apex code.

Here the sample code for unlock the record using apex . and i have added link for Idea Details from Salesfroce.  the 2 linke are very useful to you. 
https://success.salesforce.com/answers?id=90630000000gmpNAAQ

https://success.salesforce.com/ideaView?id=08730000000BrZOAA0
 
public class UnlockRecordDuringApprovalController {
    Opportunity objOpportunity;
    // String objOpp;
    String currentuserId = UserInfo.getUserId();
	
	public UnlockRecordDuringApprovalController(ApexPages.StandardController controller) {
    	objOpportunity = (Opportunity)controller.getRecord();
	}
    @future
    public static 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);
        List<PermissionSetAssignment> listPermissionSets = [SELECT Id, AssigneeId, PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId = :currentuserId AND PermissionSetId IN (SELECT Id FROM PermissionSet WHERE IsOwnedByProfile = false)];
        delete listPermissionSets;
        PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = '0PS7E000000DBGA', AssigneeId = currentuserId);		
        System.debug('psa ' + psa);
        insert psa;
		System.debug('psa7 ' + psa);
    }   
}

i hope it may help you to solve the problem. 

Thanks
karthik