Wednesday, 14 November 2018

Trigger to Delete Child On Deletion of Parent


This trigger is to Delete the Child Records on Delete of Parent Record
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
trigger DeleteContact on Account (After Delete) {
    Set<Id>AccIdSet = New Set<Id>();
    List<Contact> ConDelList = New List<Contact>();
    
    for(Account acc : trigger.old){
        AccIdSet.add(acc.id);
    }
    
    for(contact con : [Select Id, AccountId from Contact WHERE AccountId In : AccIdSet]){
        ConDelList.add(con);
    }
    
    Delete ConDelList;
}

No comments:

Post a Comment