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
BhavanaBhavana 

trigger to unlock Opportunity record submitted for approval

Please help me with the code to unlock Opportunity record submitted for approval.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post i hope that will help you
1) http://salesforceworld4u.blogspot.com/2017/07/how-to-lockunlock-record-thorough-apex.html
2) http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_approval_locks_unlocks.htm#rn_apex_approval_locks_unlocks
// Query the accounts to lock
Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%'];
// Lock the accounts
Approval.LockResult[] lrList = Approval.lock(accts, false);

// Iterate through each returned result
for(Approval.LockResult lr : lrList) {
    if (lr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully locked account with ID: ' + lr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : lr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

Let us know if this will help you
 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Bhavana
You can lock/unlock records using the new Lock/Unlock a record in Apex. You can use the Approval Class to unlock the record. 
Id[] unlockRecordIds = new Id[0];
for(SObject record: Trigger.new) {
    if(recordShouldUnlock(record)) {
        unlockRecordIds.add(record.Id);
    }
}
Approval.UnlockResult[] results = Approval.unlock(unlockRecordIds, false);
// Handle errors here

 
BhavanaBhavana
Hi Amit and Bhargavi,
Thanks for the response. Can you please help me put this code in a trigger, and it should be called right after the approval process is invoked and the record is locked(after initial submission action). 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Bhavana

Then you can do a field update in initial submission action and call  the trigger on update with a condition based on the field update you made in submission actions.For Example:
Update Status field in actions to "pending"

Write a trigger with condition : (after update) and record.status=='pending' && trigger.oldMap.get(record.Id).status!='pending'

Hope this works for your requirement.
Sanjana BheemreddySanjana Bheemreddy
Hi Amit and Bhargavi,

Am new to apex can you give me the trigger code to perform this am little bit confused ,hope that would be better.

Thanks in Advance.
Jessica BabeJessica Babe
Amit and Bhargavi,

Are you able to help me write a trigger to unlock opportunities that lock when entering the approval process?

Thank you!