I have object called opportunity and related object obj1,obj2.Once I submit the record in opportunity I need to lock the related object obj1,obj2.How can we implements this issue. Please can any one help me out.
1 Answer
Yes you can manually lock or unlock records. try with this hope this will work for you.
Create a picklist on parent object Approval Status with values 'Pending', 'Approved', 'Rejected'.
Once the record is submitted set the status to Pending, via approval process's workflow.
When approved change status. In the trigger(after update) check the Approval status then lock/unlock the child records.
Check How to run Apex trigger after successful approval workflow?
Ex: Lock records
// 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());
}
}
}
Ex: Unlock records
// Query the accounts to unlock
Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%'];
// Unlock the accounts
Approval.UnlockResult[] urList = Approval.unlock(accts, false);
// Iterate through each returned result
for(Approval.UnlockResult ur : urList) {
if (ur.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully unlocked account with ID: ' + ur.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : ur.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
-
1Yes, use the
Approval.lock(..)
andApproval.unlock(..)
methods on the related object records of the Opportunity record that entered the approval process if you need to lock them in exactly the same way as the Opportunity is locked. Another low-tech option is to add a validation rule on the related object that checks some field on the Opportunity to know if it's pending approval or not. If pending approval then validation rule prevents the user from saving the related records. Dec 18, 2015 at 6:41 -