7

Is there any way that I can prevent the undelete of an objects record? Could we implement this using a trigger, we do not have before undelete context - hence it makes this a bit difficult.

5

You need a fairly simple Apex Trigger:

trigger MyObject on My_Object__c (after undelete)
{
    for (My_Object__c record : trigger.new)
        record.addError(Label.Undelete_Not_Allowed);
}

I recommend using a Custom Label for your error message because they are configurable and hence can be changed on the fly.


I did check to see if this functionality can be performed via Validation Rule, but the field IsDeleted cannot be used in one. Otherwise, you could use the following formula:

PRIORVALUE(IsDeleted, true)
  • 1
    It should be noted, despite the correct use of a custom label for addError - the error message is not propagated to the UI in undelete triggers. It just reads "A data integrity rule or Apex trigger would fail if the record was undeleted" – Simon Lawrence Dec 6 '16 at 14:08
11

You can use addError on records in the after undelete trigger event, and it will prevent the record from being recovered.

0

I would suggest making an after delete trigger that calls Database.emptyRecycleBin

  • 1
    This approach has additional side effects. Normally deleted records can still be queried for, and hence still be archived/accessed. Making all deletions permanent is a pretty serious step and more involved than what the OP is looking for. Worth mentioning. – Adrian Larson Apr 27 '16 at 17:49
  • Thanks Charles. I was aiming to prevent undelete when somebody attempts it. Else it being in the recycle bin is fine. Moreover there are other records in the bin too, as @AdrianLarson has pointed out. Thanks – Prajith Apr 28 '16 at 6:26
  • @Stygon You can records from the recycle bin one at a time. I am not sure why you'd want to prevent undelete but persist them in recycle bin (that gives the user a strange idea of what's happening). – Charles Koppelman Apr 28 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.