• Jenny Mosunova Telit
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 2
    Likes Given
  • 1
    Questions
  • 3
    Replies
I used this code to unlock records after they enter approval process:

// 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());
        }
    }
}

Using debug logs I can tell that it is unlocking the record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex users?  Does record lock itself again after all code run?  Is there something I can do to keep the record unlocked so a user can still edit it?
The thing is, it works on Admin Profile user but not on other Profiles, despite I get success in Approval.UnlockResult for both. I assume that the problem might be in permissions, anyone knows something regarding this issue?  Thanks
I used this code to unlock records after they enter approval process:

// 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());
        }
    }
}

Using debug logs I can tell that it is unlocking the record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex users?  Does record lock itself again after all code run?  Is there something I can do to keep the record unlocked so a user can still edit it?
The thing is, it works on Admin Profile user but not on other Profiles, despite I get success in Approval.UnlockResult for both. I assume that the problem might be in permissions, anyone knows something regarding this issue?  Thanks
I was messing with unlocking opportunities.  The business requires Opportunities submitted for Approval in the "Signed and Submited for Approval" stage to be unlocked.  Currently in the UI when you do this it locks the record and places an unlock button that allows you to unlock the record only if you have an admin profile/modify all permission.  I used the sample code from the method guide to make this trigger:
 
trigger triggerUnlock on Opportunity (after update) {
    
Opportunity[] oList = [SELECT Id from Opportunity Where Id IN : Trigger.new and StageName = 'Signed and Submitted for Approval']; 

Approval.UnlockResult[] urList = Approval.unlock(oList, false);

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 opportunity 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('Opportunity fields that affected this error: ' + err.getFields());
        }
    }
}


}
Using debug logs I can tell that it is unlocking the opportunity record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex uses?  Does record lock itself again after all code has ran?  Is there something I can do to keep the record unlocked so a user can still edit it?  Thanks
 
  • August 25, 2016
  • Like
  • 1
I was messing with unlocking opportunities.  The business requires Opportunities submitted for Approval in the "Signed and Submited for Approval" stage to be unlocked.  Currently in the UI when you do this it locks the record and places an unlock button that allows you to unlock the record only if you have an admin profile/modify all permission.  I used the sample code from the method guide to make this trigger:
 
trigger triggerUnlock on Opportunity (after update) {
    
Opportunity[] oList = [SELECT Id from Opportunity Where Id IN : Trigger.new and StageName = 'Signed and Submitted for Approval']; 

Approval.UnlockResult[] urList = Approval.unlock(oList, false);

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 opportunity 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('Opportunity fields that affected this error: ' + err.getFields());
        }
    }
}


}
Using debug logs I can tell that it is unlocking the opportunity record when it is submitted for approval.  But in the UI it still has the unlock button and the record is locked.  So is the "Approval.unlock()" method used specifically to unlock the record only for apex uses?  Does record lock itself again after all code has ran?  Is there something I can do to keep the record unlocked so a user can still edit it?  Thanks
 
  • August 25, 2016
  • Like
  • 1
 Inside my trigger method i wrote below code:

public void OnBeforeUpdate(List<Account> newAccList ,List<Account> oldAccList ){
        Approval.UnlockResult[] lrList = Approval.unlock(oldAccList, false);
        system.debug('-----lrList-----'+lrList);
}
In debug log below ,I have no errors and isSucess flag set to true, but my record is still locked :
----lrList-----(Approval.UnlockResult[getErrors=();getId=00163000004DNG6;isSuccess=true;])

Has anyone used this new feature of winter 16 sucessfully?or can anyone advice why it is not working for me?