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
Ravindra MarellaRavindra Marella 

Whenever opportunity is deleted corresponding account and contact should be deleted? can any one please tell me. Thank You

Best Answer chosen by Ravindra Marella
YogeshMoreYogeshMore
Hi Ravi,

Make this method as parametrised. Like
 
public class OppTrigger{
    
     public static void afterDelete(List<Opportunity> lstopp){
        Set<Id> accIdSet = new Set<Id>();
        for(Opportunity opp : lstopp){  // error : Invalid loop variable type expected SObject was Opportunity
            accIdSet.add(opp.AccountId);
        }
        if(accIdSet.size()>0){
            List<Account> accList = [SELECT id, name
                                     FROM Account
                                     WHERE Id IN : accIdSet];
            if(accList.size()>0){
                delete accList; // deleting account here, associated contact will be deleted automatically.
            }
        }
    }
}

When your calling this method from trigger then it will be like this.

OppTrigger.afterDelete(Trigger.old);

All Answers

YogeshMoreYogeshMore
Hi Ravi,

You can achieve this by using trigger with after delete event.
Your trigger code will be like this.
 
trigger OppObjTrigger on Opportunity (After Delete) {
    
    If(Trigger.isDelete && Trigger.isAfter){
        Set<Id> accIdSet = new Set<Id>();
        for(Opportunity opp : Trigger.old){
            accIdSet.add(opp.AccountId);
        }
        
        If(accIdSet.size()>0){
            List<Account> accList = [Select Id, Name from Account Where Id IN : accIdSet];
            
            If(accList.size()>0){
                delete accList; // deleting account here, associated contact will be delete automatically.
            }
        }
    }
}

Please mark this answer as SOLVED and BEST ANSWER if it helps you.

Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || yogeshsfdc11@gmail.com
www.yogeshmore.com || Skype:-yogesh.more44
Ravindra MarellaRavindra Marella
Getting error,
Invalid loop variable type expected SObject was Opportunity
YogeshMoreYogeshMore
Hi Ravi,

I have tested from side, its working properly.
Can send me the code and whole error message with line number.
 
Ravindra MarellaRavindra Marella
// Whenever Opportunity is deleted associated account and contact should be deleted
    public static void afterDelete(){
        Set<Id> accIdSet = new Set<Id>();
        for(Opportunity opp : Trigger.old){  // error : Invalid loop variable type expected SObject was Opportunity
            accIdSet.add(opp.AccountId);
        }
        if(accIdSet.size()>0){
            List<Account> accList = [SELECT id, name
                                     FROM Account
                                     WHERE Id IN : accIdSet];
            if(accList.size()>0){
                delete accList; // deleting account here, associated contact will be deleted automatically.
            }
        }
    }
YogeshMoreYogeshMore
Hi Ravi,

Make this method as parametrised. Like
 
public class OppTrigger{
    
     public static void afterDelete(List<Opportunity> lstopp){
        Set<Id> accIdSet = new Set<Id>();
        for(Opportunity opp : lstopp){  // error : Invalid loop variable type expected SObject was Opportunity
            accIdSet.add(opp.AccountId);
        }
        if(accIdSet.size()>0){
            List<Account> accList = [SELECT id, name
                                     FROM Account
                                     WHERE Id IN : accIdSet];
            if(accList.size()>0){
                delete accList; // deleting account here, associated contact will be deleted automatically.
            }
        }
    }
}

When your calling this method from trigger then it will be like this.

OppTrigger.afterDelete(Trigger.old);
This was selected as the best answer