Trigger Challenge #6: Write a Trigger when contact is deleted, delete the corresponding account records.

0
2734
Trigger Challenge #6: Write a Trigger when contact is deleted, delete the corresponding account records.
Trigger Challenge #6: Write a Trigger when contact is deleted, delete the corresponding account records.

Hello Salesforce Cody’s, Welcome back after a long time. Let’s do a quick Beginner/Intermediate level trigger.

Trigger Challenge #6: Write a Trigger when contact is deleted, delete the corresponding account records.

trigger delContacyTrigger on Contact (after delete) {
    List<Id> accId = new List<Id>();
    for(Contact c : Trigger.old){
        accId.add(c.AccountId);
    }
    List<Account> accdelete = [select Id from Account where Id IN :accId];
    if(accdelete.size() > 0){
    	delete accdelete;
    }
}

Let me know in the comments if any doubts?