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

255 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

 

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

 

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.

Our Free Courses →

👉 Download App to Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Download App to Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration

 
 
🌩️ Download App to Get Free Course Access: -

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