Trigger: Prevent Account from Deleting if it has Opportunities associated to it. Post author:Swapnil Jaiswal Post published:January 5, 2025 Post category:Blog / Salesforce Post comments:0 Comments Example: Write a trigger logic to prevent Account from Deleting if it has Opportunities associated to it. Solution if(trigger.isBefore && trigger.isDelete) { List<Opportunity> oppr = [SELECT Id , AccountId FROM Opportunity WHERE AccountId =: Trigger.old]; if(!oppr.isEmpty() && oppr.size() > 0) { for(Account newAccount : trigger.old) { newAccount.addError('Connect delete as it has Opportunities'); } } } You Might Also Like Trigger: Access and update child value on Parent January 4, 2025 Send data from child component to parent component in lwc. January 22, 2022 Trigger: On Update Create new Task on Primary Contact of Opportunity January 5, 2025 Leave a Reply Cancel replyCommentEnter your name or username to comment Enter your email address to comment Enter your website URL (optional) Save my name, email, and website in this browser for the next time I comment.