Restrict Account Updates to Private Ownership

This trigger blocks users from saving Account updates if Ownership is set to “Private.” It includes validation logic to ensure data complies with business rules.

For further learning, explore this YouTube playlist.

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