1

I have a custom button on a record which is designed to update the record's status. There is a separate process within a different class, which is locking the record.

I need my pageRef to unlock the record, update and then re-lock. I understand that perhaps factoring the pageRef into the existing class is probably best practice, however i would like to do it independently if possible

How would this be written into the update DML?

public with sharing class BannerApplicationExtension {

    public String currentRecordID {get;set;}
    public Application__c updateApp {get;set;}
    public Opportunity updateOpp {get;set;}
    public string redirectUrl {get;set;}
    public boolean assessable {get;set;}

    public BannerApplicationExtension(ApexPages.StandardController controller) {

        currentRecordID = ApexPages.CurrentPage().getparameters().get('ID');
        redirectUrl = '/' + currentRecordID;

        updateApp = [SELECT Id,Status__c,Application_Outcome__c,Opportunity__r.id,Opportunity__r.Date_Application_Signed__c,Opportunity__r.Date_eCaf_Signed__c,Opportunity__r.stagename
                FROM Application__c WHERE id = :currentRecordID LIMIT 1];
        updateOpp = [SELECT Id,stagename,Active_Application__r.id,Lost_Reason__c
                FROM Opportunity WHERE Active_Application__r.id = :currentRecordID LIMIT 1];
    }

public PageReference approve() {
        try {updateApp.Status__c = 'Assessed';updateApp.Application_Outcome__c = 'Approved';updateopp.stagename = 'Won';
            update updateApp;
            update updateOpp;
        } catch(System.Exception e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error updating Application.'+e.getMessage()));
        }
        return null;
    }
}

EDIT - Snippet from another class which is locking record initially...

public static string submitApplication(String applicationId)
    {
        Application__c app = new Application__c();
        app.Id = applicationId;
        app.Status__c = 'Submitted';
        update app;
        Approval.lock(applicationId);
        return 'Success';
    }

1 Answer 1

2

The Approval class has methods for locking and unlocking a record:

public PageReference approve() {    
    try {
        if(Approval.unlock(updateApp).isSuccess()){
            updateApp.Status__c = 'Assessed';updateApp.Application_Outcome__c = 'Approved';updateopp.stagename = 'Won';
            update updateApp;
            update updateOpp;
            Approval.lock(updateApp);
        }
    } catch(System.Exception e) {
             ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error updating Application.'+e.getMessage()));
    }
    return null;
}

Note that you'll need to enable Apex locking/unlocking in your Setup, I believe it is under "Apex Settings."

7
  • Thanks Brian, makes sense to me. However, there seems to be a syntax error somewhere "missing } at catch" Commented Sep 27, 2017 at 22:48
  • @TheMikeyBoosh Should be fixed Commented Sep 27, 2017 at 23:05
  • Hi Brian, unfortunately after some testing it doesn't seem to work. I have added a snippet of the class which is locking the record initially. I have also removed the Approval.lock(updateApp); to see if the record was unlocking at all, it wasn't. Commented Sep 28, 2017 at 5:44
  • @TheMikeyBoosh Is the unlock call returning an error? Commented Sep 28, 2017 at 12:35
  • Hi Brian, no error, however. I think it may be profile related, does the Approval Class take object access into consideration? (modify all etc.) Commented Sep 28, 2017 at 22:03

You must log in to answer this question.

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