How to check whether a record has been locked in the Approval Process

12:48 AM

Have you been wondering how to check whether a record has been locked? I thought there might be a field which says that. But i was not able to find any such field. Instead i found that ProcessInstance table can be used to find if a record has been locked. But that looks very tedious and complex.

How about a very simple way. Here it is!!

Step1 : Create a field (say FLAG) of type Checkbox. Set the default value to UNCHECKED.

Step2: In your Approval Process in the INITIAL SUBMISSION Action , create a Field Update. Update the FLAG to TRUE.

Step3: In your FINAL APPROVAL and REJECTION Actions, create a Field Update. Update the FLAG back to FALSE.

Step4: And done!!!! You can now find whether a record has been locked simply by checking the FLAG value. Be it in your Apex Class or Visualforce Page.

Comments and queries welcome!!

8 comments

  1. This is great while dealing with a bulk of records...
    Also, Views can be created!

    Thanks for the Tip

    ReplyDelete
  2. Thats an excellent idea

    ReplyDelete
  3. I think in most of the cases orgs have "status" or similar field anyway indicating the stage in approval process, so adding this flag is not necessary.

    ReplyDelete
  4. Yes cokie.. Usually "Submitted for Approval" status is what many orgs have.. But just in case if there is not any such field then adding this flag maybe helpful rather than querying through the process tables..

    ReplyDelete
  5. The ProcessInstance's "Status = Pending" doesn't necessarily mean that the Record is locked - right??

    For example, I have Approved a record. So the ProcessInstance's Status = 'Approved'. But Now, the person next in hierarchy has to Approve it.


    So, a record is "unlocked" ONLY when the last person Approves / Rejects - right?

    ReplyDelete
  6. What if a user with Admin priveleges unlocks the record before the Approver has approved :-)

    -Raman

    ReplyDelete
  7. I am having one requirement related to triggers, i.e i want to send approval request beofore deletion reocrd , if approvar approve that then that record should delete other wise it should not be delete. i tried this but it is sending approval request before deleting record, once the approval request is sending , Reocord is also deleting before approval approving this, how can i resolve this.

    Here that record should stay upto approvar action.

    ReplyDelete
  8. I create one vf page and using username, password. And also I give the my salesforce login credentials through API in my login page. When i click the submit button it will directly go and open the salesforce login page. Please anybody give me related examples.

    ReplyDelete