You need to sign in to do that
Don't have an account?

delete associated contact records using trigger
Hi,
I have an Custom object 'Price__c' and an standard object 'Contact'. When an custom object 'Price__c' is created, 'Contact' object is also created. My question is when we delete the 'Contact' record the corresponding 'Price__c' object record must also be get deleted. Both are not related to each other but they have 'Account' object as lookup fields in both object.
I am new to coding part can someone help me.
Code:
trigger DeleteContact on Contact (before delete) {
if(Trigger.isDelete) {
for(Contact c: Trigger.old) {
Account acc = [select id from Account];
Price__c mpe = [Select Id,Name,Account__r.Name From Price__c where id =: acc.id];
Contact con = [Select Id,Name,Contact.Account.Name From Contact where id =: acc.id];
delete con;
}
}
I have an Custom object 'Price__c' and an standard object 'Contact'. When an custom object 'Price__c' is created, 'Contact' object is also created. My question is when we delete the 'Contact' record the corresponding 'Price__c' object record must also be get deleted. Both are not related to each other but they have 'Account' object as lookup fields in both object.
I am new to coding part can someone help me.
Code:
trigger DeleteContact on Contact (before delete) {
if(Trigger.isDelete) {
for(Contact c: Trigger.old) {
Account acc = [select id from Account];
Price__c mpe = [Select Id,Name,Account__r.Name From Price__c where id =: acc.id];
Contact con = [Select Id,Name,Contact.Account.Name From Contact where id =: acc.id];
delete con;
}
}
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
All Answers
Can you tell me what is the one field that is common between the Contact and Price__c records which determines that they are created together, apart from the account Id. Without that, it will not be possible to determine which Price__c record to delete when a particular Contact record is deleted.
Thanks,
Shashank
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank