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
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. Undeleting contact record should Undelete associated contact relationship record too.

shweta chadha 4shweta chadha 4
Here is my code but it is not working as expected, please help:

trigger UnDeleteCRonContactUnDeletion on Contact(After Undelete) {
   ContactMasterHandler_Undelete con = new ContactMasterHandler_Undelete();
    con.undeleteContactRelationshipsByContact(trigger.new);
}

public class ContactMasterHandler_Undelete {
    
    public void undeleteContactRelationshipsByContact(list <Contact> List_Contacts)
    {
        list<id> ContactIds=new list<id>();
        for(Contact contactVar:List_Contacts)
    {
        ContactIds.add(contactVar.id);
    } 
         list<Contact_Relationship__c> listOfContacts=[select id from Contact_Relationship__c where IsDeleted = True and Contact__c in :ContactIds All Rows] ;
        undelete listOfContacts;
    }

}     
Laxmi Pandey 14Laxmi Pandey 14
The undelete should also restore the related records automatically provided you dont delete the related record first and then the parent. In that case you will need to undelete both the parent and related record and it will again appear as attached as it was before.