• suresh s 34
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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