function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vijay Zutshi 2Vijay Zutshi 2 

Before Delete Trigger not deleting all records

My Trigger is as follows:
trigger contactRelationship on Contact (after insert, before delete) {
    LIST<Contact> newContact = new LIST<Contact>();
    LIST<Contact_Relationship__c> createRelationship = new  LIST<Contact_Relationship__c>();
    SET<ID> contactRelId = new SET<ID>();
    if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Contact createContact : Trigger.new) {
                newContact.add(createContact);
                if(createContact.Contact_Relationship__c == TRUE) {
                    Contact_Relationship__c newContactRelationship = new                      Contact_Relationship__c();
                    newContactRelationship.Contact_Relationship__c =                            createContact.Id;
                    newContactRelationship.Name =                                                           createContact.LastName;
                    createRelationship.add(newContactRelationship);
                    contactRelId.add(newContactRelationship.Id);                                            
                }
            }
            insert createRelationship;
        }
    }
    LIST<Contact> deleteContact = new LIST<Contact>();
    SET<ID> contactDeleteId = new SET<ID>();
    SET<ID> allContactId = new SET<ID>();    
    if(Trigger.IsBefore && Trigger.IsDelete) {
        //if(Trigger.IsDelete) {
            for(Contact allContact : Trigger.old) {
                deleteContact.add(allContact); 
                contactDeleteId.add(allContact.Id);
                 //Contact_Relationship__c delContRel = new Contact_Relationship__c();   
            }
            LIST<Contact> listContactDelete = [SELECT Id, LastName
                                               FROM Contact
                                               WHERE Id IN :contactDeleteId];
            system.debug('contact' + listContactDelete);
            //if(listContactDelete != NULL && listContactDelete.size()>0) {
              //delete listContactDelete;  
            //}
            
            LIST<Contact_Relationship__c> deleteContactRelationship = [SELECT Id, Name
                                                                       FROM Contact_Relationship__c 
                                                                       WHERE Contact_Relationship__c IN :contactDeleteId];
            system.debug('relation' + deleteContactRelationship);
            for(Contact_Relationship__c delRel :deleteContactRelationship) {
            update deleteContactRelationship;
            }            
            //if(deleteContactRelationship != NULL && deleteContactRelationship.size()>0) {
                //delete deleteContactRelationship;
            //}             
        //}
    }
}


I have Contact object under which there is a Contact Relationship custom object. When I add a contact it gets also added to the custom object. This part is working ok. Now I add another record to the custom object which is under the same Contact. So there are multiple records in the custom object. So when I delete a Contact then it should also delete all records in the custom object which is related to the same contact. This is not happening and I would appreciate help on this as I am new to this.
Thanks
Vijay Zutshi
ShirishaShirisha (Salesforce Developers) 
Hi Vijay,

Greetings!

I would suggest you to capture the debug logs to narrow down the code which is causing the issue.Also,if you would like to delete the records which are related to the Contact then you can query and delete the records.

Reference:https://developer.salesforce.com/forums/?id=906F0000000AhP6IAK

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
suresh s 34suresh s 34
Hi Vijay,

Please use after delete for deleting contact relationships of an contact you tried to delete instead before delete. we use before delete generally to make any validations to restrict deletion of record. we use after delete for performing any DML operations of related objects. Please mark it as best answer in case it worked with after delete operation.

Regards,
Somu Suresh 
Vijay Zutshi 2Vijay Zutshi 2
I tried using After delete but it still does the same thing. It is deleting ony 1 record from Contact Relationship. There are 2 records or more than 2 which needsto be deleted not just 1. Please advise.

Thanks
Vijay