0

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 1

2

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

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Approval_LockResult.htm

// 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

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Approval_UnlockResult.htm

// 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());
        }
    }
}
2
  • 1
    Yes, use the Approval.lock(..) and Approval.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.
    – Doug Ayers
    Dec 18, 2015 at 6:41
  • Yes I agree both are points. working on that
    – Jansi
    Dec 18, 2015 at 6:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .