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
anshuanshu 

trigger on delete

i have written trigger on opportunity and it should delete things in related list , but output is not expected

triggercode:

 

trigger deleteopportunity on Opportunity (before delete) {

list<string> opp1= new List<String>();
list<string> d1= new List<String>();
list<d__c>d;
      for(Opportunity o : Trigger.old){
        
    opp1.add ( o.Id );
  }
  d= [select id,name,Opportunity__c from d__c where Opportunity__c IN:opp1];
  for(d__c d2:d){
   d1.add(d2.id);
   }

clsdeleteopportunity.deleteall(d1);

}

 

class code:

public with sharing class clsdeleteopportunity{
    @future
 public static void deleteall(list<string> oppid)
 {

   
list<d__c>d=new List<d__c>();

list<string> d1=new List<string>();

d= [select id,name,Opportunity__c from d__c where Opportunity__c IN:oppid ];
 for(d__c d11:d){
        d1.add(d11.id);
        
    }
if(!d.isEmpty()){
delete d;
}

*werewolf**werewolf*

That's pretty hard to read when you call all your variables "d," but I can see the problem.  You are passing a bunch of ids of d__c objects to the deleteall method.  However in the deleteall method you are treating those IDs as if they are opportunity IDs, and querying them as if they are opportunity IDs.  As such, your query returns no rows, and you delete nothing.

 

I would suggest also that you make your variable names a bit more descriptive; that might have helped you spot this issue.

anshuanshu

hi , thanks for reply , but still i have not got the solution, can you explain in detail

 

trigger deleteopportunity on Opportunity (before delete) {

 

list<string> opp1= new List<String>();

list<string> pro1= new List<String>();

list<projct__c> pro;

      for(Opportunity o : Trigger.old){

 

    opp1.add ( o.Id );

  }

  pro= [select id,name,Opportunity__c from projct__c where Opportunity__c IN:opp1];

  for(projct__c p:pro){

   pro1.add(p.id);

   }

 

clsdeleteopportunity.deleteall(pro1);

 

}

 

class code:

public with sharing class clsdeleteopportunity{

    @future

 public static void deleteall(list<string> oppid)

 {

 

list<projct__c>pro=new List<projct__c>();

list<string> pro1=new List<string>();

pro= [select id,name,Opportunity__c from projct__c where Opportunity__c IN:oppid ];

 for(projct__c p:pro){

        pro1.add(p.id);

 

    }

if(!pro.isEmpty()){

delete pro;

}

 

Inside project , i have another object , like project1__c so , this object should also be deleted along with opportunity,

but i am getting too many soql queries error?

help

SteveBowerSteveBower

Can't you:

 

trigger Delete_Opportunity_Related_Projcts on Opportunity (before delete) {

    delete [select id from projct__c where Opportunity__c IN :trigger.oldmap.keyset()];

}

 

Best Torque

 

p.s. Late night typing... please forgive if I'm just missing something...

 

p.p.s. Assuming there are multiple projects for each Opportunity, could you instead make the Opportunity__c relationship to the Opportunity be a Master-Detail relationship?   Then cascading delete would just take care of this for you?