24

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?

6
  • Can you please be more specific? what record? any record? custom record? are you refering to a custom button you add? please try to add some more information to the answer, I am sure it will give you more answers.
    – Saariko
    Commented Sep 10, 2012 at 8:45
  • I have an approval process on the Custom object and also it has two custom buttons on the same record. Once the record gets locked i don't want user to hit these two custom buttons on the locked record.How can access a record is locked or not using Apex? Commented Sep 10, 2012 at 9:42
  • What do you mean by locked? can you be more specific please
    – Saariko
    Commented Sep 10, 2012 at 11:40
  • Once record is submitted for the Approval it gets locked.I don't want the users to click the custom buttons on the records as long as the record is locked. Commented Sep 10, 2012 at 12:40
  • 2
    The platform has changed since you accepted your answer and there is a newer, more correct answer than what you have accepted (posted by Shane).
    – Adrian Larson
    Commented May 19, 2016 at 18:34

5 Answers 5

6

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!

2
  • 1
    We can get the Record status by Querying on the related History object. Commented Sep 12, 2012 at 12:32
  • 1
    The attempt to write and catch exception method would fail if the user has 'Modify All' enabled on their profile for that object. Commented Oct 30, 2012 at 9:00
26

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.

1
  • When trying to use Approval.isLocked(recordId) in controller constructor the page is not loaded and says Insufficient Privileges. It looks like some users can not use this class to check if record is locked Commented Mar 27, 2017 at 16:00
6

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;
}
6

This is how I solved this issue:

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

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.

You must log in to answer this question.

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