<p> ///Note: Salesforce has built in validation rule to prevent deletion Accounts with related Contacts/ Opportunities.<br> ///Another option is to use Record triggered flow<br><br> trigger preventAccountDeletion on Account (before delete) {<br> // Check if the trigger is before delete<br> if (Trigger.isBefore && Trigger.isDelete) {<br> // Query for account IDs that have related contacts<br> List<AggregateResult> accountWithContact = [<br> SELECT AccountId<br> FROM Contact<br> WHERE AccountId IN :Trigger.oldMap.keySet()<br> GROUP BY AccountId<br> HAVING COUNT(Id) > 0<br> ];<br> // Throw an error if there are accounts with related contacts<br> if (accountWithContact.size() > 0) {<br> Trigger.old[0].addError(accountWithContact.size() + ' Accounts cannot be deleted because they have related contacts.');<br> }<br> }<br> }<br> </p>