I want to prevent users from deleting the records even if they have modify all data permission. Please suggest.


You should handle this scenario of deletion based on Custom Permission set assigned to the users, otherwise, even system admin cannot delete the records.

So, in the trigger of before delete context verify custom permission set and then allow records to delete.

Following code helps to check Custom permissions from Apex.

Boolean hasCustomPermission = FeatureManagement.checkPermission('your_custom_permission_api_name');

Refer Custom Permissions for more information.

You can also look for configurable way to checking this using flows.

  • I assume you mean to add this logic into a trigger, yes? I didn't go into details but this is one of those "extra logic" points I was hinting at in my answer. – Phil W Dec 16 '19 at 12:13
  • @PhilW, I prefer to handle this in trigger – Santanu Boral Dec 16 '19 at 12:19
  • Right, which fits with what I was suggesting. The two answers together will give a good approach. – Phil W Dec 16 '19 at 12:36

Simply add a trigger on the object against before delete, iterate each instance and add an error to it stating deletion is not permitted.

Use of a trigger prevents it ever being overridden other than by modifying or deleting the trigger. Of course, you can add extra logic in the trigger to allow deletions in very specific scenarios if you do still need to support deletions at some point (e.g. to apply custom permissions or similar approaches to allow specific users to still be able to do the deletion).

For the trigger it is trivial to support this with shared code, like:

public with sharing class DisallowedDeletionHandler {
    public static void handle(List<SObject> deletions) {
        for (SObject deletion : deletions) {
            deletion.addError('cannot delete');

Each trigger looks something like:

trigger DisallowAccountDeletion on Account (before delete) {

A non-code alternative is to do the same sort of thing using a Process Builder flow.

In either case you need a specific definition per object type.

  • Thanks for solution ,but i have couple of object says more thann 10.its quite repeated tsk to create atrigger for each object.is there any way where i can do it generic – sam Dec 16 '19 at 10:34

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.