0

The update here completes without issue. That's not what I was expecting after locking the record. Please advise.

 public static Map<String, String> undoNotAwarded(String[] quoteIdsArray){
    Map<String, String> toastVariantToMessage = new Map<String, String>();
    String vendorList = '';

    List<Custom_Quote__c> quotes = [SELECT Id, Vendor_Name__r.Name, Status__c
                                          FROM Custom_Quote__c
                                          WHERE Id IN :quoteIdsArray];
    lockQuotes(quotes);

    for(Custom_Quote__c quote : quotes) {
        quote.Status__c = 'Ready for Review';
        vendorList += quote.Vendor_Name__r.Name + ', ';
    }

    try {
        update quotes;
    } catch(Exception e) {
        system.debug(e.getMessage());
        String message = 'An error occurred. Emails were not sent to: ' + vendorList;
        toastVariantToMessage.put('error',message);
        return toastVariantToMessage;
    }
    
    String toastMessage = 'Status set to \'Ready for Review\' on these quotes: ' + vendorList;
    toastVariantToMessage.put('success',toastMessage);
    return toastVariantToMessage;
}

LockQuotes Private Method

    private static void lockQuotes(List<Custom_Quote__c> quotes){

    Approval.LockResult[] approvalLock = Approval.Lock(quotes, false);

    for(Approval.LockResult al : approvalLock) {
        if (al.isSuccess()) {
            System.debug('Successfully Locked Quote with ID: ' + al.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : al.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Quote fields that affected this error: ' + err.getFields());
            }
        }
    }
}
4
  • Are you sure the record was locked? Here, Approval.lock(quotes, false) will keep going even if there were errors. Make sure you check your logs.
    – sfdcfox
    Commented Jan 26, 2023 at 4:22
  • Does it matter if the Approval Process is inactive? @sfdcfox Commented Jan 26, 2023 at 7:03
  • Also, got success in the logs: 23:12:30:038 USER_DEBUG [222]|DEBUG|Successfully Locked Quote with ID: a4cDL000000hdds @sfdcfox Commented Jan 26, 2023 at 7:13
  • According to developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/… "Salesforce admins can edit locked records". Apex classes are executed with admin-like permissions, so that may explain it. Commented Jan 26, 2023 at 11:40

1 Answer 1

0

I believe Sander de Jong was correct in his comment: According to developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/… "Salesforce admins can edit locked records". Apex classes are executed with admin-like permissions, so that may explain it.

In my apex, it was locking from the beginning. The problem was that I was testing with my Sys Admin User. I created a different user with Profile that had Edit permission on Quote object. When that user updates the record, lock worked.

You must log in to answer this question.

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