// 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());
}
}
}
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).
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'
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
Let us know if this will help you
You can lock/unlock records using the new Lock/Unlock a record in Apex. You can use the Approval Class to unlock the record.
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).
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.
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.
Are you able to help me write a trigger to unlock opportunities that lock when entering the approval process?
Thank you!