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.
// 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. } } }
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.
Make this method as parametrised. Like
When your calling this method from trigger then it will be like this.
OppTrigger.afterDelete(Trigger.old);
All Answers
You can achieve this by using trigger with after delete event.
Your trigger code will be like this.
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
Invalid loop variable type expected SObject was Opportunity
I have tested from side, its working properly.
Can send me the code and whole error message with line number.
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.
}
}
}
Make this method as parametrised. Like
When your calling this method from trigger then it will be like this.
OppTrigger.afterDelete(Trigger.old);