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.
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; }
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.
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;
}
}