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
Raj88Raj88 

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;
}
}
Best Answer chosen by Raj88
ShashForceShashForce
The code you wrote is not properly bulkified. I'm pasting here a bulkified sample code for your requirement, which you may have to slightly modify for your need:

trigger DeleteContact on Contact (before delete) {
    
    list<price__c> pricelist = new list<price__c>();
    list<price__c> pricesToDelete = new list<price>();
    list<Id> accIds = new list<Id>();
    
    for(contact con:trigger.old){
        accIds.add(con.accountId);
    }
    
    pricelist = [select Id,name,account__c,Primary_Role__c,Secondary_Role__c,Email__c,Last_Name__c from pricelist where account__c in :accIds];
    
    for(price__c pr:pricelist){
        for(contact c:trigger.old){
            if( (pr.account__c==c.accountId) && (pr.primary_role__c==c.Primary_Contact_Type__c) && (pr.Secondary_Role__c==c.Contact_Type__c) && (pr.Email__c==c.Email) && (pr.Last_Name__c==c.LastName) ){
                pricesToDelete.add(pr);    
            }
        }
    }
    
    delete pricesToDelete;
}

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

ShashForceShashForce
Hi,

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
Raj88Raj88
Price__c has 'Primary_Role__c,Secondary_Role__c,Email__c,Last_Name__c' and Contact has 'Primary_Contact_Type__c,Contact_Type__c,Email,LastName'. These fields are match with each other.
ShashForceShashForce
The code you wrote is not properly bulkified. I'm pasting here a bulkified sample code for your requirement, which you may have to slightly modify for your need:

trigger DeleteContact on Contact (before delete) {
    
    list<price__c> pricelist = new list<price__c>();
    list<price__c> pricesToDelete = new list<price>();
    list<Id> accIds = new list<Id>();
    
    for(contact con:trigger.old){
        accIds.add(con.accountId);
    }
    
    pricelist = [select Id,name,account__c,Primary_Role__c,Secondary_Role__c,Email__c,Last_Name__c from pricelist where account__c in :accIds];
    
    for(price__c pr:pricelist){
        for(contact c:trigger.old){
            if( (pr.account__c==c.accountId) && (pr.primary_role__c==c.Primary_Contact_Type__c) && (pr.Secondary_Role__c==c.Contact_Type__c) && (pr.Email__c==c.Email) && (pr.Last_Name__c==c.LastName) ){
                pricesToDelete.add(pr);    
            }
        }
    }
    
    delete pricesToDelete;
}

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
This was selected as the best answer