Find your content:

Search form

You are here

How to check if a record is locked or not in Apex during an approval process

 
Share

I have an approval process on a Custom object and also two custom buttons on the same record.

Once the record goes into the approval process, it gets locked. During that period, I want to disable/hide the two custom buttons.

How can I check if the record/custom object is locked or not using Apex?

And how can I disable/hide the buttons?


Attribution to: Keepcalmncode

Possible Suggestion/Solution #1

This came out in Winter16. It's part of the approval class

Approval.isLocked(recordId)

This method can also accept List<Id>, SObject, or List<SObject>.

You can also now lock and unlock them from Apex. See the documentation on the Approval class.


Attribution to: Shane McLaughlin

Possible Suggestion/Solution #2

The IsLocked field is not currently exposed via the API or SOQL, but there's an idea on the IdeaExchange to add that functionality.

In the meantime, you can work around that by created a custom field on the object called In_Approval_Process__c, and edit your approval process to set this field when the record is submitted for approval and clear it after approval is complete.

You can't totally hide or disable the buttons, but you can prevent them from doing anything. Switch the button's Content Source from URL to Execute Javascript and do something like the following:

var linkUrl = '/whatever/your/button/url/would/have/been';
var isLocked = {!sObject.In_Approval_Process__c};
if (isLocked) {
  alert('This record is locked for approval.  Please try again after it has been accepted.');
} else {
  window.location.href = linkUrl;
}

Attribution to: Benj

Possible Suggestion/Solution #3

This is how I solved this issue:

public Boolean doesOpportunityHavePendingApproval() { return ![ Select Id From ProcessInstance WHERE TargetObjectId =: theOpportunity.Id AND Status = 'Pending' ].isEmpty(); }


Attribution to: Clark.Hruska

Possible Suggestion/Solution #4

To hide the buttons you can do the following:

  1. Make a copy of your current page layout and call it e.g. "old page layout name + locked". On this page layout remove the buttons.
  2. Create a record type which is a copy of the current record type.
  3. Assign the new page layout to the new record type.
  4. Trigger a field update for the "Initial Submission Actions". The field to update is the "Record Type"-field. Define as the new record type value the new record type you created. SF will load the related page layout where are no buttons.

That's it.

  1. Dont forget to make another field updates for the old record type in the Recall-, Final Approval- and Final Reject Actions as required.

Attribution to: Michael Haase

Possible Suggestion/Solution #5

I believe it's possible to query for the existence of an Approval Request related to your record, I believe this is via: ProcessInstance. Try checking out:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

Additionally, you could write a method to attempt to update the record, and trap the exception message that comes from the record being locked and hide the buttons based on that result!


Attribution to: jordan.baucke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1298

My Block Status

My Block Content