You need to sign in to do that
Don't have an account?
Vijay 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
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
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
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
Thanks
Vijay