Skip to content
  • Surfmyindia |
  • Lifedreamguide |
  • w3webmart |
  • Overstockphoto |
  • News24globalworld |
  • News24classictimes |
  • Incriediableindia |
  • TechW3web |
  • W3Force ..
w3web.net
Sign Up to Get Free Code Access β†’ |
  • Home
  • Tutorial
    • Lightning Component
    • Salesforce LWC
    • Salesforce Integration
    • Visualforce
    • Trigger
    • JQuery
  • Get Free Courses
  • Integration
  • Aura Comp
  • Salesforce LWC
  • Visualforce
  • Trigger
  • JQuery
  • React Js
  • More
    • About Us
    • Contact Us
    • Sitemap
    • Course Pricing
    • Blog
    • Gallery
    • Most Popular Articles
    • Download E-Book
    • YouTube Channel


Do not allow delete Account, if any of related Contact associated with Account in Salesforce | Prevent the deletion of Account which have related contact through apex trigger in Salesforce

January 14, 2025January 30, 2024 by Vijay Kumar
2,655 views

Hey guys, today in this post we are going to learn about How to Do not allow delete Account, if any of related Contact associated with Account in Salesforce.

 

Final Output β†’

 

Other related post that would you like to learn in Salesforce

πŸ‘‰ Download Free Ebook β†’

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com
  • Apex trigger to create and count the number of child records associated with parent object based on custom field when parent record is created/updated in Salesforce
  • Apex Trigger to Send a Custom Visualforce Component Email Template when Record is Created on Custom Object in Salesforce
  • Trigger to Update the Custom Last Modified Date Field in the Parent Object When the Records in the Child Object is Updated Using Apex Trigger in Salesforce
  • Trigger to update count of child records with custom field of parent object | count of child records on parent for lookup relationship uses of apex trigger in Salesforce
  • Create rollup summary using Apex trigger on custom object | rollup summary trigger for count of child records on custom object in Salesforce example
  • Create Duplicate Rule & Matching Rule to Prevent Insert/Update Duplicate Records on Contact sObject based on Contact Email and Contact Phone in Salesforce
  • Apex trigger on Account to avoid creation of duplicate record if the account with same phone exists in the system in Salesforce
  • Write a trigger on contact to update parent record when child is updated using apex trigger in Salesforce
  • Write a trigger whenever Opportunity is deleted the corresponding Account and Contact should be deleted uses of Apex trigger in Salesforce
  • Write a trigger on Contact to Prevent the user to create duplicate Contact based on Phone if Phone number is already exist on related account in Salesforce
  • Write Validation rule for StageName of opportunity sObject that don’t allow the user to move Previous/Next Stage based on User/Profile (Except specific profile of user) in Salesforce

 

Create Apex Trigger β†’

Step 1:- Create Apex Controller : AccountTrigger.apxt

SFDX:Create Apex Trigger>> New >> AccountTrigger.apxt

myAccountTrigger.apxt [Apex Trigger]


  1.     TRIGGER AccountTrigger ON Account (BEFORE DELETE, after DELETE) {
  2.     IF(TRIGGER.isBefore && TRIGGER.isDelete){
  3.         Set<Id> setId = TRIGGER.oldMap.keySet();
  4.         List<Contact> conObjList = [SELECT Id, Name, FirstName, LastName, AccountId FROM Contact WHERE AccountId IN:setId];
  5.  
  6.         Map<Id, Contact> conMap = NEW Map<Id, Contact>();
  7.  
  8.         FOR(Contact con:conObjList){
  9.             conMap.put(con.AccountId,con);
  10.         }
  11.  
  12.         FOR(Account acc:TRIGGER.old){
  13.             IF(conMap.containsKey(acc.Id)){
  14.                 acc.addError('You can not delete this Account, if any of associated contact with this Account!');
  15.             }
  16.         }
  17.  
  18.     }ELSE IF(TRIGGER.isAfter){
  19.  
  20.     }
  21.  
  22. }

Further post that would you like to learn in Salesforce

  • Trigger on contact to update a account custom field | Write a trigger on contact and update the account custom field uses of apex trigger in Salesforce
  • Create a Apex Trigger on Account to Update the Associated Contact Account Name Null Whenever Account Record is Updated in Salesforce
  • Trigger to check duplicate name to custom object in Salesforce | how to prevent duplicate records based on multiple fields through apex trigger in Salesforce
  • Write a Apex trigger to Add the Contact First Name and Last Name to Associated Account Custom Field Whenever the Contact inserted or Updated in Salesforce
  • Trigger to update parent records field when child record is updated | update parent field from child using apex trigger
  • How to call invocable method (@InvocableMethod) from APEX Controller to convert automatic lead to Account, Contact and Opportunity Uses of Flow Builder/Flow Action in Salesforce
  • Write a trigger to update parent account phone number whenever the contact phone number is updated using trigger handler and helper class in Salesforce
  • Trigger on custom object to prevent delete the related list record based on lookup relation whenever trying to delete parent record, throw an error message using apex class handler trigger in Salesforce
  • Write a trigger on lead to prevent duplicate records based on lead email Whenever the records is inserted Or updated using apex class handler trigger in Salesforce
  • Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever New Contact is Created in Salesforce

 

FAQ (Frequently Asked Questions)

How do I Delete related contacts in Salesforce?

To delete a contact related to an entitlement, delete the associated entitlement contact records or remove the roll-up summary field from the Contact object. The deleted contact is moved to the Recycle Bin. If you undelete the contact, any associated records are restored, including any inactive Self-Service usernames.

Why we Cannot Delete a user in Salesforce?

Each account is connected to a specific role, gaining access to certain levels of your Salesforce database. Deleting an account would corrupt data uploaded to the space and compromise your company's database. You wouldn't be able to use specific files, and the related data would also be ruined.

How do related contacts work in Salesforce?

The contact and its primary account have a direct relationship. But you can add other accounts to the contact. These secondary account-contact relationships are indirect. Contacts to Multiple Accounts works with person accounts.

Related Topics | You May Also Like

  • Before creating a Lead, we will check Email, if email already exist, then we will display error message, do not allow create duplicate Lead.   || we do not allow duplicate Lead, if email already exist, then we will display error message on LeadBefore creating a Lead, we will check Email, if email already exist, then we will display error message, do not allow create duplicate Lead. || we do not allow duplicate Lead, if email already exist, then we will display error message on Lead
  • Upon Contact record creation if LeadSource Field is having value as β€˜Web’ or β€˜Phone Inquiry’ then Populate Level as β€˜Primary’ through Apex Trigger in SalesforceUpon Contact record creation if LeadSource Field is having value as β€˜Web’ or β€˜Phone Inquiry’ then Populate Level as β€˜Primary’ through Apex Trigger in Salesforce
  • Upon Contact record creation if LeadSource Field is having value as β€˜Web’ or β€˜Phone Inquiry’ then Populate Level as β€˜Primary’Upon Contact record creation if LeadSource Field is having value as β€˜Web’ or β€˜Phone Inquiry’ then Populate Level as β€˜Primary’
  • When an Account record is created then Create a related Contact automatically through Apex Trigger in SalesforceWhen an Account record is created then Create a related Contact automatically through Apex Trigger in Salesforce
  • Write a apex trigger on Account, whenever Active is “Yes”, before save the record we will be check the child object (Opportunity) StageName should not be “Closed Lost”, if any of record of child record get “Closed Lost”, it will be through an error.Write a apex trigger on Account, whenever Active is “Yes”, before save the record we will be check the child object (Opportunity) StageName should not be “Closed Lost”, if any of record of child record get “Closed Lost”, it will be through an error.
  • Update Parent field when Child record is Updated or Inserted in Salesforce | Trigger updating parent with child value when a field on the child is changedUpdate Parent field when Child record is Updated or Inserted in Salesforce | Trigger updating parent with child value when a field on the child is changed
  • Update all Email of Contact related list to the associated Account if Account is updated in Salesforce |  How to update all Contact record related to Account in SalesforceUpdate all Email of Contact related list to the associated Account if Account is updated in Salesforce | How to update all Contact record related to Account in Salesforce
  • While creation of Account if Phone is null then throw an error message, Here we are applying a custom validation in apex triggerWhile creation of Account if Phone is null then throw an error message, Here we are applying a custom validation in apex trigger

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com
 
  
πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Vijay Kumar

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Categories Trigger, Tutorial Tags account and contact trigger in salesforce, create a trigger to validate that no more than 3 contact, delete all contacts belonging to accounts, How to delete an account record associated with a contact, How to restrict delete for profiles in salesforce, if it has 2 or more contacts, Impact of Deleting Accounts in Salesforce, Issues with deleting or merging accounts and contacts, only the system admin user should be able to delete the task, prevent account from deleting, prevent delete trigger salesforce, prevent deletion of an account if there is any contact, prevent deletion of an account if there is any contact related to that account, Prevent the deletion of Account which have related contact, write a trigger, write a trigger user should not be able to delete any account with associated contact
Update all Email of Contact related list to the associated Account if Account is updated in Salesforce | How to update all Contact record related to Account in Salesforce
Do not allow edit the Opportunity record if the record is created 7 days back from System.Today() | Do not allow a user to modify the Opportunity record if the Opportunity is created before 7 days from today

Archives β†’

Categories β†’

  • Agentforce (4)
  • Blog (4)
  • More (7)
  • Tutorial (326)
    • Formula Field (1)
    • JQuery (13)
    • Lightning Component (60)
    • React Js (15)
    • Salesforce Integration (41)
    • Salesforce LWC (115)
    • Trigger (57)
    • Validation Rule (9)
    • Visualforce (16)
  • Uncategorized (4)

Global Visitors β†’

Popular Posts β†’

  • Salesforce REST API Integration Using Postman – Complete Tutorial | Understanding How to Call Salesforce Web Service | Salesforce REST API: A Step-by-Step Guide | Tech W3Web 3.33 views per day
  • add page header or footer into a visualforce -- w3web.net How to make header and footer fixed and repeat on Visualforce page rendered as PDF in Salesforce | How to add header and footer in Visualforce page rendered as PDF in Salesforce 3 views per day
  • trigger to create account Automatically whenever contact is is created -- w3web.net Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever New Contact is Created in Salesforce | trigger to create account automatically whenever contact is created in salesforce 2.50 views per day
  • create lightning datatable row actions in lwc -- w3web.net Creating a Lightning Datatable with Row Actions and Display a Modal Popup on Click View Icon Button in Salesforce Lightning Web Component – LWC | how to create lightning datatable row actions in lwc 2.33 views per day
  • create form fields horizontally in lwc -- w3web.net How to align lightning-input form elements horizontally uses of slds-form-element_horizontal css and lightning-card tag in Lightning Web Component – LWC | How to create horizontal input label using slds-form-element/slds-form-element_horizontal... 2.33 views per day
  • Do not allow delete Account, if any of related Contact associated with Account in Salesforce | Prevent the deletion of Account which have related contact through apex trigger in Salesforce 2.33 views per day
  • display different types of toast message in lwc -- w3web.net How to display different types of custom toast message as Error, Success, Info and warning toast notifications on click button, uses of ShowToastEvent property in Salesforce Lightning Web Component — LWC | display custom toast notification in... 2 views per day
  • change stateful button value in lwc -- w3web.net How to change the state of button value as ‘follow/unfollow’ or ‘like/dislike’ through onclick handler method uses of ‘lightning-button-stateful’ tag elements in Salesforce LWC (Lightning Web Component) | How to c... 2 views per day

Get Free Courses β†’

right side banner -- www.surfmyindia.com
right side banner -- overstockphoto.blogspot.com

Join Our Newsletter β†’

Loading
right side banner -- www.lifedreamguide.com

Recent Posts β†’

  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce
  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce
  • Apex Trigger Whenever Case is: Inserted/Updated/Deleted/Undeleted We need to count Cases based on Priority: High, Medium, Low And update counts on Account
right side banner -- www.w3webmart.com


header banner -- www.overstockphoto.blogspot.com
  • Follow Me β†’
➑ : Facebook
➑ : Twitter
➑ : Linkedin
➑ : Instagram
➑ : Reddit
➑ : Forcetalks
➑ : Pinterest
➑ : Github
➑ : Medium
➑ : Tumblr
➑ : Telegram
 

🧩 Get Free Courses

πŸ‘‰ Get Complementary β†’

right side banner -- www.w3webmart.com
header banner -- www.overstockphoto.blogspot.com

Recent Posts β†’

  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce
  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce
  • Apex Trigger Whenever Case is: Inserted/Updated/Deleted/Undeleted We need to count Cases based on Priority: High, Medium, Low And update counts on Account
  • Apex Trigger Whenever Opportunity is: Inserted/Updated/Deleted/Undeleted We need to: Calculate total Amount of all Opportunities Where StageName = ‘Closed Won’ And update it on Account
  • Apex Trigger Whenever Opportunity is Inserted/Updated/Deleted/Undeleted We need to count how many Opportunities have: StageName = ‘Closed Won’ And update that count on Account.
  • Apex Trigger Whenever Contact is Inserted/Updated/Deleted/Undeleted We need to count Contacts based on LeadSource and update Account, LeadSource values: Web, Phone Inquiry, Partner Referral, Advertisement
header banner -- www.lifedreamguide.com

Popular Posts β†’

  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce 7 views
  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce 6 views
  • create lightning-card container with custom tab in lwc -- w3web.net How to create lightning-card container with custom tab functionality uses a button in the actions slot where we are opening a lightning popup inside a custom form, displaying plain text in the footer slot in Salesforce LWC | How to apply lightning-card to create stylized container around a grouping of information to display different section like Header, Body, Footer and button actions with open a popup inside form in Salesforce LWC (Lightning Web Component) 3 views
  • How to Navigate to External Web Page in LWC – Lightning Web Component | How to use Navigation Service (NavigationMixin.Navigate) to Navigate to External Web Page in LWC 3 views
  • Surfmyindia |
  • Lifedreamguide |
  • w3webmart |
  • Overstockphoto |
  • News24classictimes |
  • TechW3web |
  • Refund Policy |
  • Delivery Policy |
  • Privacy Policy |
  • Term & Conditions
© 2026 w3web.net • Built with GeneratePress

Chat Now