Restrict Account Updates to Private Ownership

In Salesforce, data quality and governance go hand in hand with flexible CRM configurations. While profiles, validation rules, and workflows handle a lot of security and automation, sometimes you need to go deeper and implement custom Apex logic to block updates that don’t align with business policy.

In this blog, we focus on one such scenario — preventing users from changing the Ownership field to “Private” on an Account when specific criteria are met. Specifically, when the Account’s Industry is set to “Agriculture” and the Type is “Prospect,” we want to block updates that change Ownership to Private.

You’ll learn how to implement this logic using a before update Apex trigger that compares old and new values, and stops the update with a clear validation message if the rule is violated.

🧠 Why Ownership Changes Matter


The Ownership field on the Account object often dictates data visibility, access rights, and process flows tied to record access. If users can freely change Ownership to Private, it can:

  • Disrupt sharing rules

  • Break internal workflows

  • Hide records from teams that need access

  • Compromise reporting accuracy

In certain industries or business models (like agriculture in this case), it’s critical to maintain consistent access settings and prevent unauthorized changes to sensitive fields like Ownership.

This Apex-based solution puts that control directly into your org’s logic, ensuring clean governance and consistent behavior without relying solely on UI-level validations.

🔍 What You’ll Learn in This Blog


  • How to write a before update Apex trigger to compare old vs. new field values

  • How to enforce custom business rules tied to multiple field values (Industry, Type, Ownership)

  • How to use addError() to prevent data updates in real time

  • Why Apex is better suited than validation rules for conditional change detection

  • How to build reusable trigger handler methods with clear logic separation

  • How to maintain a clean and scalable trigger framework

⚙️ How the Trigger Works – Deep Dive


This solution uses a method in the AccountTriggerHandler class that is called during the before update context. The key steps are:

  1. Loop through each Account in Trigger.new.

  2. For each Account, check if the Industry is "Agriculture" and the Type is "Prospect".

  3. Compare the old value of Ownership (from Trigger.oldMap) with the new value.

  4. If the Ownership field has changed and the new value is "Private", use addError() to block the update and show the message:

    “Ownership cannot be modified”

This kind of validation is not possible with declarative tools like validation rules alone because they don’t track changes between old and new values. That’s where Apex shines — especially when field-level audit logic is required.

🎯 Real-World Use Cases for Ownership Restrictions


  • Prevent reps from hiding records by setting them to private ownership

  • Lock critical access settings for Accounts at certain stages of the sales lifecycle

  • Enforce stricter visibility for regulated industries like healthcare, finance, agriculture, etc.

  • Create fine-grained field update controls across teams, profiles, or regions

This trigger pattern is especially useful for compliance-focused orgs or those with complex record-sharing requirements.

👨‍💻 Developer & Admin Tips


  • You can expand this logic by adding a check for user roles, profiles, or record types

  • Use Custom Metadata to make field values (like “Agriculture” or “Private”) configurable without hardcoding

  • Combine with field history tracking to log change attempts for audit purposes

  • Add test methods to validate both allowed and blocked scenarios across data variations

🎥 Visual Guide – Watch It in Action

To help you fully grasp the logic and implement it in your dev org, we’ve included a YouTube playlist with hands-on demos, walkthroughs, and real-time coding. Whether you’re a beginner learning your first Apex trigger or a pro looking to clean up your rule-based logic, these videos are built to make every concept click.

Solution:

public class Account TriggerHandler {
      public static void handleBeforeUpdateActivities (List<Account> newRecords, Map<Id,Account> oldMap) {
           for (Account accRecord : newRecords) {
                      if (accRecord.Industry == ‘Agriculture’ && accRecord.Type == ‘Prospect’) {
                      //check if value of ownership was updated, and if it was updated, is the new value Private?
                               if (oldMap.get (accRecord.Id). Ownership != accRecord.Ownership &&
                               accRecord.Ownership == ‘Private’) {
                                              accRecord.addError(‘Ownership cannot be modified’);
                              }
                     }
           }
     }
}

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 :)