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]
|
TRIGGER AccountTrigger ON Account (BEFORE DELETE, after DELETE) {
IF(TRIGGER.isBefore && TRIGGER.isDelete){
Set<Id> setId = TRIGGER.oldMap.keySet();
List<Contact> conObjList = [SELECT Id, Name, FirstName, LastName, AccountId FROM Contact WHERE AccountId IN:setId];
Map<Id, Contact> conMap = NEW Map<Id, Contact>();
FOR(Contact con:conObjList){
conMap.put(con.AccountId,con);
}
FOR(Account acc:TRIGGER.old){
IF(conMap.containsKey(acc.Id)){
acc.addError('You can not delete this Account, if any of associated contact with this Account!');
}
}
}ELSE IF(TRIGGER.isAfter){
}
}
Further post that would you like to learn in Salesforce
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
Our Free Courses β
π 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 |