Block Account Deletion with Related Cases

This trigger restricts Account deletion if there are any related Case records, maintaining data dependencies and relationships.

Learn more here: YouTube playlist.

Solution:

trigger AccountTrigger on Account (before delete) {
          if(Trigger.isDelete){ if(Trigger.isBefore){
                                                              AccountTriggerHandler.preventDelIfHasRelatedOpp(Trigger.old);
                         }
            }
}
public class AccountTriggerHandler{
              public static void preventDelIfHasRelatedCase(List accList){ Set idSet= new Set();
                          for(Account acc:accList){ idSet.add(acc.Id);
                          }
                         for(Account acc:[SELECT Id,(SELECT Id FROM Cases)FROM Account WHERE Id IN:idSet]){
                                      if(acc.Cases.size() > 0){ acc.addError(‘You can not delete account where cases are
available ‘);
                                       }
                       }
         }
}

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