Prevent Deletion of Active Accounts

In any growing Salesforce implementation, protecting critical data from accidental deletion is essential. Account records often serve as the foundation for related objects like Opportunities, Contacts, Cases, and more. Deleting an Account without proper checks can result in loss of valuable history, reporting discrepancies, and broken relationships across your CRM.

In this blog, we introduce a straightforward Apex trigger that helps maintain data integrity by blocking users from deleting Account records if the custom field “Active” is set to “Yes.” This safeguard ensures that only non-active Accounts are eligible for deletion, adding an extra layer of control to your org’s data management process.

The trigger runs in the before delete context and leverages an error message—sourced from a custom label—to communicate clearly with the user when deletion is not allowed.

🧠 Why This Trigger Is Important


Deleting records like Accounts can have far-reaching consequences. When the Account is actively engaged in business processes, open opportunities, or ongoing support, its removal can cause:

  • Incomplete historical data

  • Disruption in pipeline or sales workflows

  • Inaccurate reporting and forecasting

  • Loss of context for customer interactions

  • Unintended deletion of child records

By preventing deletion of Accounts marked as Active, this trigger:

  • Safeguards high-value business data

  • Encourages users to update status instead of deleting

  • Enforces internal rules for Account lifecycle management

  • Supports long-term CRM stability and trust in reporting

This is a must-have automation for any org that prioritizes data security and accuracy.

🔍 What This Blog Covers


  • How to use a before delete Apex trigger to prevent unwanted record removal

  • How to enforce business rules based on custom field values

  • Why “Active = Yes” is used as the condition for deletion restriction

  • How to provide clear user feedback using custom labels

  • The role of trigger handler classes in clean, maintainable code

  • Where this type of automation fits into your Salesforce data protection strategy

This logic is simple yet powerful—it helps you control what users can and cannot do, all while maintaining a smooth user experience.

🎯 Real-World Use Cases for This Trigger


  • Sales operations preventing the deletion of live customer accounts

  • Finance teams ensuring account-linked invoices or contracts aren’t lost

  • Support teams safeguarding active service relationships

  • Customer success teams retaining visibility into ongoing engagements

  • Admin teams enforcing policy without relying on complex permission sets

Whether you’re working in a startup or an enterprise-level Salesforce org, having automated controls over critical records makes your CRM stronger and safer.

👨‍💻 Developer & Admin Tips


This trigger uses a straightforward condition:

  • It runs during the before delete lifecycle, which means it can block the deletion before it happens

  • It checks the Active field on each Account record

  • If the field value is “Yes,” the system raises an error and prevents deletion

  • The error message is defined in a custom label to allow for easier translation or future edits without touching code

Using a trigger handler method to wrap this logic keeps your code modular and future-ready. This also allows for the rule to be easily extended to include exceptions, additional conditions, or integrations with approval workflows.

If your business processes involve specific user roles that should be allowed to delete active Accounts, you can further enhance this logic by incorporating profile or role-based conditions.

Be sure to test for edge cases such as:

  • Bulk deletions from list views

  • Deletion via the API

  • Automated deletions via integrations or scheduled jobs

🎥 Step-by-Step Visual Demo – YouTube Playlist Available


Want to see how it all works in a real org? Watch the full implementation in the Salesforce Makes Sense YouTube playlist. The tutorial includes:

  • A complete walkthrough of the trigger and handler method

  • Explanation of the custom label setup

  • Live testing of the restriction with sample records

  • Admin tips for customizing the logic

The playlist is perfect for admins and developers alike who want to see practical Apex in action without overcomplication.

Solution:
trigger AccountTrigger on Account (before delete)
              { if(Trigger.isDelete){ if(Trigger.isBefore){
                                       AccountTriggerHandle.preventDel(Trigger.old);
                           }
            }
}
public class AccountTriggerHandle{ public static void
            preventDel(List<Account> accList){ for(Account acc : accList){ if(acc.Active__c == ‘Yes’){
                 acc.addError(Label.Prevent_Account_Deletion);
                                              }
                               }
                 }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)