+ Start a Discussion
krushna Amilkanthwarkrushna Amilkanthwar 

using trigger i want delete account relactated contact

bhaveshjogikbhaveshjogik
Hello,

You can try following code:
 
trigger deleteAccountRecord on Contact (after delete) {


    RecordType donorRecType = [SELECT ID FROM RecordType WHERE sObjectType = 'Contact' AND DeveloperName = 'Donor_Contact'];
    if(Trigger.isDelete){
        List<ID> accIds = new List<ID>();
        for(Contact contactToDelete: Trigger.old) {
            if (contactToDelete.RecordTypeId != donorRecType.Id ) {
                  
                   accIds.add(contactToDelete.accountId);
            }
        }
      
       
       List<Account> DelAccountRecordList = [select id from Account where Id IN:accIds];

        if(DelAccountRecordList.size() >0){
           delete DelAccountRecordList ;
         
        }
     }
  }

Let me know if you are getting any error.

Thanks,
Bhavesh

 
Nandigam RajeshNandigam Rajesh
Hi,
PFB
trigger DeleteContact on Contact(after delete)
{
    List<account> accs= [SELECT contactid FROM account WHERE Contactid IN:Trigger.OldMap.keyset()];
    delete accs;
}


Regards
Rajesh​​​​​​​
Deepali KulshresthaDeepali Kulshrestha
Hi krushna,

Please try this code:

trigger TriggerTo_Delete_ChildRecords on Account (before delete) {
    
    //To store parent ids
    list<id> AccountIds=new list<id>();
    for(Account accountVar:trigger.old)
    {
        AccountIds.add(accountVar.id);
    }  
    //Collecting all child records related to Parent records
    list<contact> listOfContacts=[select id from Contact where accountid in :AccountIds];
    system.debug('listOfContacts'+listOfContacts);
    //deleting child records
    delete listOfContacts;
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha