+ Start a Discussion
shweta chadha 4shweta chadha 4 

I have two objects contact and contact Relationship. Contact has a lookup on contact relationship. Contact relationship is a related list on contact object. deleting contact record should delete associated contact relationship record too.

here is my code:

trigger DeleteContact on Contact (after delete) {
    if(trigger.isbefore)
    {
        
        if (trigger.isdelete)
        {
             set<id> idset = new set<id>();
        
        for (Contact c : Trigger.old)
    {
        idset.add(c.Id);
    }
    
         
   List <Contact_Relationship__c> CR = [Select id from Contact_Relationship__c where id IN:idset];
   
           delete CR;        }
    }}
 
Best Answer chosen by shweta chadha 4
Maharajan CMaharajan C
Sorry Some small changes in code because u r created the Custom Contact Lookup field in Child:
 
trigger TriggerChildRecords on Contact (before delete) {
    //To store parent ids
    list<id> AccountIds=new list<id>();
    for(Contact accountVar:trigger.old)
    {
        AccountIds.add(accountVar.id);
    }  
    //Collecting all child records related to Parent records and Please use the Custom lookfield name in SOQL below don't use ID here am using my field API name is Contact__c
    list<Contact_Relationship__c> listOfContacts=[select id from Contact_Relationship__c where Contact__c in :AccountIds];
    system.debug('listOfContacts'+listOfContacts);
    //deleting child records
    delete listOfContacts;
}

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj

All Answers

shweta chadha 4shweta chadha 4
But it is not working as expected.
 
Nayana KNayana K
trigger DeleteContact on Contact (after delete) 
{
    if(Trigger.isAfter && Trigger.isDelete)
    {
        set<id> idset = new set<id>();
        for (Contact c : Trigger.old)
        {
            idset.add(c.Id);
        }
        /*Assuming Contact__c is the field name of lookup on Contact_Relationship__c */
        List <Contact_Relationship__c> CR = [Select id from Contact_Relationship__c where Contact__c IN:idset];
        if(!CR.isEmpty())
        {
            delete CR;
        }
    }
}
Nayana KNayana K
trigger DeleteContact on Contact (after delete) 
{
    if(Trigger.isAfter && Trigger.isDelete)
    {
		/*Assuming Contact__c is the field name of lookup on Contact_Relationship__c */
		List <Contact_Relationship__c> CR = [Select id from Contact_Relationship__c where Contact__c IN: Trigger.oldMap.keySet()];
        if(!CR.isEmpty())
        {
            delete CR;
		}
	}
}

This is optimized code.
shweta chadha 4shweta chadha 4
Thanks Nayana , however i still see deleitng the record not deleting the associated contact relationship record.
Nayana KNayana K
Oh turn on debug log and can you please post log here:
trigger DeleteContact on Contact (after delete) 
{
    if(Trigger.isAfter && Trigger.isDelete)
    {
     system.debug('===Trigger.oldMap===='+Trigger.oldMap);
		/*Assuming Contact__c is the field name of lookup on Contact_Relationship__c */
		List <Contact_Relationship__c> CR = [Select id from Contact_Relationship__c where Contact__c IN: Trigger.oldMap.keySet()];

 system.debug('===CR ===='+CR );
        if(!CR.isEmpty())
        {
            delete CR;
       }
   }
}

 
Maharajan CMaharajan C
Hi Shweta,

Please try the below code:

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

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
 
Maharajan CMaharajan C
Sorry Some small changes in code because u r created the Custom Contact Lookup field in Child:
 
trigger TriggerChildRecords on Contact (before delete) {
    //To store parent ids
    list<id> AccountIds=new list<id>();
    for(Contact accountVar:trigger.old)
    {
        AccountIds.add(accountVar.id);
    }  
    //Collecting all child records related to Parent records and Please use the Custom lookfield name in SOQL below don't use ID here am using my field API name is Contact__c
    list<Contact_Relationship__c> listOfContacts=[select id from Contact_Relationship__c where Contact__c in :AccountIds];
    system.debug('listOfContacts'+listOfContacts);
    //deleting child records
    delete listOfContacts;
}

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
This was selected as the best answer
shweta chadha 4shweta chadha 4
Thanks Raj, it seems to be working. I would like to understand why did we strore Account IDs instead of contact Ids?
shweta chadha 4shweta chadha 4
Thanks Nayana for your help too.